列表

详情


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

给你一个函数  f(x, y) 和一个目标结果 z,函数公式未知,请你计算方程 f(x,y) == z 所有可能的正整数 数对 xy。满足条件的结果数对可以按任意顺序返回。

尽管函数的具体式子未知,但它是单调递增函数,也就是说:

函数接口定义如下:

interface CustomFunction {
public:
  // Returns some positive integer f(x, y) for two positive integers x and y based on a formula.
  int f(int x, int y);
};

你的解决方案将按如下规则进行评判:

 

示例 1:

输入:function_id = 1, z = 5
输出:[[1,4],[2,3],[3,2],[4,1]]
解释:function_id = 1 暗含的函数式子为 f(x, y) = x + y
以下 x 和 y 满足 f(x, y) 等于 5:
x=1, y=4 -> f(1, 4) = 1 + 4 = 5
x=2, y=3 -> f(2, 3) = 2 + 3 = 5
x=3, y=2 -> f(3, 2) = 3 + 2 = 5
x=4, y=1 -> f(4, 1) = 4 + 1 = 5

示例 2:

输入:function_id = 2, z = 5
输出:[[1,5],[5,1]]
解释:function_id = 2 暗含的函数式子为 f(x, y) = x * y
以下 x 和 y 满足 f(x, y) 等于 5:
x=1, y=5 -> f(1, 5) = 1 * 5 = 5
x=5, y=1 -> f(5, 1) = 5 * 1 = 5

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/* * // 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) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2022-11-18 11:10:12

/** 
 * This is the declaration of customFunction API.
 * @param  x    int
 * @param  x    int
 * @return 	    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)
 */

func findSolution(customFunction func(int, int) int, z int)(ans [][]int) {
    for x:=1;x<=1000;x++{
        if customFunction(x,1)>z{
            break
        }
        left,right := 1, 1000
        for left<=right{
            mid:=(left+right)/2
            flag := customFunction(x,mid)
            if flag>z{
                right = mid-1
            }else if flag<z{
                left = mid+1
            }else{
                ans = append(ans, []int{x,mid})
                break
            }
        }
    }
    return
}

python3 解法, 执行用时: 44 ms, 内存消耗: 15 MB, 提交时间: 2022-11-18 11:09:43

"""
   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 Solution:
    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
        ans = []
        for x in range(1,1001):
            if customfunction.f(x, 1) > z:
                break
            left, right = 1,1000
            while left<=right:
                mid = (left+right)//2
                flag = customfunction.f(x, mid)
                if flag > z:
                    right = mid-1
                elif flag < z:
                    left = mid+1
                else:
                    ans.append([x,mid])
                    break
        return ans

golang 解法, 执行用时: 0 ms, 内存消耗: 1.8 MB, 提交时间: 2022-11-18 11:07:22

/** 
 * This is the declaration of customFunction API.
 * @param  x    int
 * @param  x    int
 * @return 	    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)
 */

func findSolution(customFunction func(int, int) int, z int) [][]int {
    ans := [][]int{}
    x, y := 1, 1000
    var v int
    for x <= 1000 && y >= 1 {
        v = customFunction(x, y)
        if v < z {
            x++
        } else if v > z {
            y--
        } else {
            ans = append(ans, []int{x, y})
            x++
        }
    }
    return ans
}

python3 解法, 执行用时: 56 ms, 内存消耗: 14.8 MB, 提交时间: 2022-11-18 11:01:16

"""
   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 Solution:
    def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
        x, y = 1, 1000
        ans = []
        while x <= 1000 and y >= 1:
            v = customfunction.f(x, y)
            if v < z:
                x += 1
            elif v > z:
                y -= 1
            else:
                ans.append([x, y])
                x += 1
        return ans

java 解法, 执行用时: 2 ms, 内存消耗: 39.4 MB, 提交时间: 2022-11-18 10:57:30

/*
 * // 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>> result = new ArrayList<>();
        int x=1, y=1000;
        while (x<=1000 && y>=1) {
            if (customfunction.f(x, y) > z) {
                y--;
            } else if (customfunction.f(x, y) < z) {
                x++;
            } else {
                result.add(Arrays.asList(x, y));
                x++;
            }
        }
        return result;
    }
}

java 解法, 执行用时: 130 ms, 内存消耗: 39.2 MB, 提交时间: 2022-11-18 10:56:59

/*
 * // 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>> ans = new ArrayList<>();
		for (int i = 1; i < 1001; i++) {
			for (int j = 1; j < 1001; j++) {
				if (customfunction.f(i, j) == z) {
					ans.add(Arrays.asList(new Integer[] { i, j }));
				}
			}
		}
		return ans;
	}
}

上一题