Leetcode 1237. 找出给定方程的正整数解
1. 问题描述
2. 思路
二分查找
固定x,二分查找y
3. 代码
/**
* 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 {
var res = make([][]int, 0)
for x := 1; x <= z; x++ {
left, right := 1, 1000
for left <= right {
mid := left + (right - left) / 2
temp := customFunction(x, mid)
if temp == z {
tempRes := []int{x, mid}
res = append(res, tempRes)
break
} else if temp < z {
left = mid + 1
} else {
right = mid - 1
}
}
}
return res
}