题目描述:
LeetCode 第1237题 找出给定方程的正整数解
类型简单
思路:
以1为下限,z为上线遍历查找
代码如下:
/*
* // This is the custom function interface.
* // You should not implement it, or speculate about its implementation
* class CustomFunction {
* public:
* // 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)
* int f(int x, int y);
* };
*/
class Solution {
public:
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
vector<int>temp={0,0};
vector<vector<int>>res;
int x=1,y=z;
int cur=z;
while(x<=z){
if(customfunction.f(x,y)==z){
cur=y;
temp[0]=x;
temp[1]=y;
res.push_back(temp);
x++;
}
else {
y--;
if(y<1){
y=cur;
x++;}
}
}
return res;
}
};