題目:
給出一個(gè)函數(shù) f(x, y) 和一個(gè)目標(biāo)結(jié)果 z,請你計(jì)算方程 f(x,y) == z 所有可能的正整數(shù) 數(shù)對 x 和 y。
給定函數(shù)是嚴(yán)格單調(diào)的,也就是說:
f(x, y) < f(x + 1, y)
f(x, y) < f(x, y + 1)
函數(shù)接口定義如下:
interface CustomFunction {
public:
// Returns positive integer f(x, y) for any given positive integer x and y.
int f(int x, int y);
};
如果你想自定義測試,你可以輸入整數(shù) function_id 和一個(gè)目標(biāo)結(jié)果 z 作為輸入,其中 function_id 表示一個(gè)隱藏函數(shù)列表中的一個(gè)函數(shù)編號,題目只會告訴你列表中的 2 個(gè)函數(shù)。
你可以將滿足條件的 結(jié)果數(shù)對 按任意順序返回。
示例 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 的范圍內(nèi)。
在 1 <= x, y <= 1000 的前提下,題目保證 f(x, y) 是一個(gè) 32 位有符號整數(shù)。
題目的理解:
思考這個(gè)題目盡然用了1個(gè)小時(shí),反復(fù)在看這個(gè)真的是簡單難度嘛。最后發(fā)現(xiàn)被“1 <= z <= 100,題目保證 f(x, y) == z 的解處于 1 <= x, y <= 1000 的范圍內(nèi)?!闭`導(dǎo)了,還有就是對customfunction.f判斷的理所當(dāng)然有除法運(yùn)算。WRONG,其實(shí)通過f(x, y) < f(x, y + 1)就可以明白除法是不可能的。
所以x,y的取值范圍僅僅是1到z了。
python實(shí)現(xiàn)
class Solution:
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
result = list()
for s in range(1, z + 1):
for t in range(1, z + 1):
if customfunction.f(s, t) == z:
result.append([s, t])
break
return result
提交

Panic
// END 堅(jiān)定自己的判斷和信念!