盐山建网站,竹子系统做的网站可以优化么,安卓编程软件,轻量级服务器wordpress1. 题目
给出一个函数 f(x, y) 和一个目标结果 z#xff0c;请你计算方程 f(x,y) z 所有可能的正整数 数对 x 和 y。
给定函数是严格单调的#xff0c;也就是说#xff1a;
f(x, y) f(x 1, y)
f(x, y) f(x, y 1)函数接口定义如下#xff1a;
interface Cu…1. 题目
给出一个函数 f(x, y) 和一个目标结果 z请你计算方程 f(x,y) z 所有可能的正整数 数对 x 和 y。
给定函数是严格单调的也就是说
f(x, y) f(x 1, y)
f(x, y) f(x, y 1)函数接口定义如下
interface CustomFunction {
public:// Returns positive integer f(x, y) for any given positive integer x and y.int f(int x, int y);
};如果你想自定义测试你可以输入整数 function_id 和一个目标结果 z 作为输入其中 function_id 表示一个隐藏函数列表中的一个函数编号题目只会告诉你列表中的 2 个函数。
你可以将满足条件的 结果数对 按任意顺序返回。
示例 1
输入function_id 1, z 5
输出[[1,4],[2,3],[3,2],[4,1]]
解释function_id 1 表示 f(x, y) x y示例 2
输入function_id 2, z 5
输出[[1,5],[5,1]]
解释function_id 2 表示 f(x, y) x * y提示
1 function_id 9
1 z 100
题目保证 f(x, y) z 的解处于 1 x, y 1000 的范围内。
在 1 x, y 1000 的前提下题目保证 f(x, y) 是一个 32 位有符号整数。来源力扣LeetCode 链接https://leetcode-cn.com/problems/find-positive-integer-solution-for-a-given-equation 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
2. 解题
类似题目搜索二维矩阵 x1,y1,相当于矩阵左上角x1000y1000,相当于矩阵右下角 class Solution {
public:vectorvectorint findSolution(CustomFunction cf, int z) {int x 1, y 1000, val;vectorvectorint ans;while(x1000 y 1){val cf.f(x,y);if(val z)x;else if(val z)y--;else{ans.push_back({x,y});x;}}return ans;}
};or
class Solution {
public:vectorvectorint findSolution(CustomFunction cf, int z) {int x 1000, y 1, val;vectorvectorint ans;while(x1 y1000){val cf.f(x,y);if(val z)y;else if(val z)x--;else{ans.push_back({x,y});x--;}}return ans;}
};