如果你想自定义测试,你可以输入整数 function_id 和一个目标结果 z 作为输入,其中 function_id 表示一个隐藏函数列表中的一个函数编号,题目只会告诉你列表中的 2 个函数。
你可以将满足条件的 结果数对 按任意顺序返回。
class Solution { public List<List<Integer>> findSolution(CustomFunction customfunction, int z) { } }对于传入进来的customfunction,他里面包含一个f(x,y)方法.因为这个函数是严格单调的 我们可以对x和y进行暴力遍历.最坏情况就为O(N^2); 1)当遍历的时候使用双重for循环进行遍历 2)一旦遍历到f(i,j) > z的情况,则直接跳出即可,因为这个函数是严格单调的,后面的i和j只会越来越大, 值肯定比z大. 3)当遍历的f(i,j) = z的时候我们把他放入到结果List中即可.
public List<List<Integer>> findSolution(CustomFunction customfunction,int z){ List<List<Integer>> result = new ArrayList<>(); //双重for暴力遍历 for(int i = 1;i <= 1000;i++){ for(int j = 1;j <= 1000;j++){ //等于z直接放入到list中 if(customfunction.f(i, j) == z){ result.add(Arrays.asList(i,j)); } //一旦大于z后,直接跳出. if(customfunction.f(i, j) > z){ break; } } } return result; }