CS-Notes icon indicating copy to clipboard operation
CS-Notes copied to clipboard

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

Open SaltedReed opened this issue 2 years ago • 1 comments

应该把变量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;
 }

SaltedReed avatar Jan 01 '23 01:01 SaltedReed

同!,得改成long

emthaibara avatar Jan 17 '23 11:01 emthaibara