Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chencl1986 opened this issue Sep 5, 2024 · 0 comments

Comments

@chencl1986
Copy link
Owner

原题链接:
1237. 找出给定方程的正整数解

解题思路:

  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(n^2)$
  • 空间复杂度:$O(1)$。返回值不计入空间复杂度
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant