CyC2018 / CS-Notes

:books: 技术面试必备基础知识、Leetcode、计算机操作系统、计算机网络、系统设计

Home Page:http://cyc2018.xyz

Repository from Github https://github.comCyC2018/CS-NotesRepository from Github https://github.comCyC2018/CS-Notes

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

SaltedReed opened this issue · 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;
 }

同!,得改成long