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 个函数。

你可以将满足条件的 结果数对 按任意顺序返回。

示例

方法一:暴力法

基本思路:根据题意可以知道,我们需要为一个函数提供两个参数,当参数等于某个给定的值时,将参数记录下来,所以可以运用二重循环,遍历两个参数的所有取值,当函数结果等于给定的值时,就将参数记录下来。
优化:又题意可知,函数时单调递增的,所以当函数结果大于等于给定的值时,就可以结束当前循环,因为根据函数单调的性质,后面的值必然也是大于给定的值的。
时间复杂度: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)
 *     public int f(int x, int y);
 * };
 */

class Solution {
    public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
        List<List<Integer>> list = new ArrayList<>();
        for(int i = 1; i <= 1000; i++){
            if(customfunction.f(i, 1) > z){
                break;
            }
            for(int j = 1; j <= 1000; j++){
                if(customfunction.f(i, j) == z){
                    List<Integer> list2 = new ArrayList<>();
                    list2.add(i);
                    list2.add(j);
                    list.add(list2);
                    break;
                }
                else if(customfunction.f(i, j) > z) break;
            }
        }
        return list;
    }
}

二分查找

基于暴力解法的优化,将内层循环变为二分查找
时间复杂度:O(nlog(n))

/*
 * // 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)
 *     public int f(int x, int y);
 * };
 */

class Solution {
    public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
        List<List<Integer>> list = new ArrayList<>();
        for(int i = 1; i <= 1000; i++){
            if(customfunction.f(i, 1) > z){
                break;
            }
            int left = 1;
            int right = 1000;
            while(left <= right){
                int mid = (left + right) / 2;
                int temp = customfunction.f(i, mid);
                if(temp == z){
                    List<Integer> list2 = new ArrayList<>();
                    list2.add(i);
                    list2.add(mid);
                    list.add(list2);
                    break;
                }
                else if(temp < z){
                    left = mid + 1;
                }
                else right = mid - 1;
            }
        }
        return list;
    }
}

双指针法

既然函数是单调有序的,那么自然可以使用双指针法来代替二重循环
时间复杂度:O(N)

/*
 * // 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)
 *     public int f(int x, int y);
 * };
 */

class Solution {
    public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
        List<List<Integer>> list = new ArrayList<>();
        int left = 1;
        int right = 1000;
        while(left <= 1000 && right >= 1){
            int temp = customfunction.f(left, right);
            if(temp == z){
                List<Integer> list2 = new ArrayList<>();
                list2.add(left);
                list2.add(right);
                list.add(list2);
                //这里使用left++或者right--都行,不然会造成死循环
                right--;
            }
            else if(temp < z){
                left++;
            }
            else{
                right--;
            }
        }
        return list;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值