likes
comments
collection
share

LeetCode题解:1237. 找出给定方程的正整数解,枚举,详细注释

作者站长头像
站长
· 阅读数 14

原题链接: leetcode.cn/problems/fi…

解题思路:

  1. 根据题意,1 <= x, y <= 1000,因此可以暴力枚举所有xy,提供给函数计算
  2. customfunction.f(x, y) === z的结果全部存储并返回即可
/**
 * @param {CustomFunction} customfunction
 * @param {integer} z
 * @return {integer[][]}
 */
var findSolution = function(customfunction, z) {
  let result = [] // 存储结果

  // 枚举所有x,y,并使用函数计算结果
  for (let x = 1; x <= 1000; x++) {
    for (let y = 1; y <= 1000; y++) {
      // 如果计算结果等于z,就存储x,y
      if (customfunction.f(x, y) === z) {
        result.push([x, y])
      }
    }
  }

  return result
};

复杂度分析:

  • 时间复杂度:O(n2)O(n^2)O(n2)
  • 空间复杂度:O(1)O(1)O(1)。返回值不计入空间复杂度
转载自:https://juejin.cn/post/7201391887147843641
评论
请登录