LeetCode—— 1237 找出给定方程的正整数解

问题描述

给出一个函数  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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

执行结果

代码描述

方案一:

思路:测试了一下,当customfunction = 1~9 时,各自的结果,说明内置的函数类型还挺多的。

当customfunction = 1, x+y; customfunction = 2, x*y, 

customfunction = 3, 输出结果:[[1,4],[2,1]]

customfunction = 4, 输出结果:[[1,2],[4,1]]

customfunction = 5, 输出结果:[[1,2],[2,1]]

customfunction = 6,7, 输出结果:[]

customfunction = 8, 输出结果:[[1,5]]

customfunction = 9, 输出结果:[[5,1]]

i 与 j 每次递增1个,然后算是否f(i,j)==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<vector<int>> res;
        vector<int> temp;
        for(int i = 1; i <= z; ++i)
        {
            for(int j = 1; j <= z; ++j)
            {
                if(customfunction.f(i,j) == z)
                {
                    temp.push_back(i);
                    temp.push_back(j);
                    res.push_back(temp);
                    temp.clear();
                }
            }
        }
        return res;
    }
};

方案二: 由于 i j 都会使得函数单调递增,所以取i 最小值,取 j的最大值,然后两个值封分别向各自的终点靠近。

参考  240 搜索二维矩阵II

/*
 * // 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<vector<int>> res;
        vector<int> temp;
        int i = 1, j = 1000;
        while(i <= 1000 && j > 0)
        {
            if(customfunction.f(i, j) == z)
            {
                temp.push_back(i);
                temp.push_back(j);
                res.push_back(temp);
                temp.clear();
                //++i;    // i 增,或者 j降,结果都一样。
                --j;
            }
            else if(customfunction.f(i, j) < z)
                ++i;
            else
                --j;
        }
        return res;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值