参考:
https://leetcode-cn.com/problems/find-positive-integer-solution-for-a-given-equation/solution/xiang-jie-bao-li-er-fen-yu-shuang-zhi-zhen-fa-by-q/
"""
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 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):
return x * y
class Solution:
def findSolution2(self, customfunction: 'CustomFunction', z: int):
answers = list()
for i in range(1,z + 1):
for j in range(1,z + 1):
z_ = customfunction.f(i,j)
if z_ == z:
answers.append((i,j))
break
elif z_ > z:
break
return answers
# 双指针法
def findSolution(self, customfunction: 'CustomFunction', z: int):
ans, x, y = [], 1, z
while x <= z and y >= 1:
res = customfunction.f(x, y)
if res < z:
x += 1
elif res > z:
y -= 1
if res == z:
ans.append([x, y])
x += 1
y -= 1
return ans
if __name__ == '__main__':
x = Solution()
print(x.findSolution(customfunction=CustomFunction(), z=5))
```
package T1237找出给定方程的正整数解;
import java.util.ArrayList;
import java.util.List;
interface 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)
public int f(int x, int y);
};
public class Solution {
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
List<List<Integer>> answers = new ArrayList<>();
int left =1;
int right = z;
while (left <= z && right >= 1) {
int z_ = customfunction.f(left, right);
if (z == z_) {
List<Integer> answer = new ArrayList<>();
answer.add(left);
answer.add(right);
answers.add(answer);
left += 1;
right -= 1;
} else if (z_ < z) {
left += 1;
} else {
right -= 1;
}
}
return answers;
}
}