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题解/双指针2 代码有误 #1219

Open
SaltedReed opened this issue Jan 1, 2023 · 1 comment
Open

Leetcode题解/双指针2 代码有误 #1219

SaltedReed opened this issue Jan 1, 2023 · 1 comment

Comments

@SaltedReed
Copy link

应该把变量i, j, powSum的类型改为long,否则会出现整数溢出,2147483600这个测试用例过不了。
修改后如下:

public boolean judgeSquareSum(int target) {
     if (target < 0) return false;
     long i = 0, j = (long) Math.sqrt(target);
     while (i <= j) {
         long powSum = i * i + j * j;
         if (powSum == target) {
             return true;
         } else if (powSum > target) {
             j--;
         } else {
             i++;
         }
     }
     return false;
 }
@emthaibara
Copy link

同!,得改成long

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

2 participants