【leetcode】1237. Find Positive Integer Solution for a Given Equation
2024-09-05 13:18:27
题目如下:
Given a function
f(x, y)
and a valuez
, return all positive integer pairsx
andy
wheref(x,y) == z
.The function is constantly increasing, i.e.:
f(x, y) < f(x + 1, y)
f(x, y) < f(x, y + 1)
The function interface is defined like this:
interface CustomFunction {
public:
// Returns positive integer f(x, y) for any given positive integer x and y.
int f(int x, int y);
};For custom testing purposes you're given an integer
function_id
and a targetz
as input, wherefunction_id
represent one function from an secret internal list, on the examples you'll know only two functions from the list.You may return the solutions in any order.
Example 1:
Input: function_id = 1, z = 5
Output: [[1,4],[2,3],[3,2],[4,1]]
Explanation: function_id = 1 means that f(x, y) = x + yExample 2:
Input: function_id = 2, z = 5
Output: [[1,5],[5,1]]
Explanation: function_id = 2 means that f(x, y) = x * yConstraints:
1 <= function_id <= 9
1 <= z <= 100
- It's guaranteed that the solutions of
f(x, y) == z
will be on the range1 <= x, y <= 1000
- It's also guaranteed that
f(x, y)
will fit in 32 bit signed integer if1 <= x, y <= 1000
解题思路:看到1 <= x, y <= 1000时,就可以意识到O(n^2)的复杂度是可以接受的,那么两层循环计算一下吧。
代码如下:
"""
This is the custom function interface.
You should not implement it, or speculate about its implementation
class CustomFunction:
# Returns f(x, y) for any given positive integers x and y.
# Note that f(x, y) is increasing with respect to both x and y.
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
def f(self, x, y): """
class Solution(object):
def findSolution(self, customfunction, z):
"""
:type num: int
:type z: int
:rtype: List[List[int]]
"""
res = []
for x in range(1,1001):
for y in range(1,1001):
if customfunction.f(x,y) == z:
res.append([x,y])
elif customfunction.f(x,y) > z:
break
return res
最新文章
- centos7+mono4.2.3.4+jexus5.8.1跨平台起飞
- form 表单基础知识
- IIS部署遇到的一些问题
- Linux下C语言编程实现spwd函数
- app性能测试点、安全测试点总结
- 分享几个Javascript 封装方法
- vue 使用总结
- get the execution time of a sql statement.
- linux 解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot images will not be built问题
- iOS:删除小程序
- [闲的蛋疼系列]从零开始用TypeScript写React的UI组件(0)-先写一个Button??
- 蓝桥杯 倍数问题(dfs,枚举组合数)
- s33 cobbler自动化安装系统
- CSS 中伪类的顺序
- 配置Codeblocks
- 原生JavaScript技巧大收集
- [转]一千行MySQL学习笔记
- GIS-013-Cesium Terrain 数据生成
- string类型版本号比较
- 使用infinite-scroll实现Ghost博文列表的滚动加载
热门文章
- java按某属性分组并计算相关属性的和。
- CDH6.2安装之YUM方式
- PTA(Basic Level)1015.德才论
- C++练习 | 类的继承与派生练习(1)
- python并发编程-进程间通信-Queue队列使用-生产者消费者模型-线程理论-创建及对象属性方法-线程互斥锁-守护线程-02
- 通过设置访问密码查看Tomcat服务器运行状态
- 数据绑定-@ CookieValue
- 101、Service 之间如何通信?(Swarm08)
- Flask-migrate基本使用方法
- 07 Python中zip(),map(),filter(),reduce()用法