您的位置:首页 > 其它

[LeetCode]633. Sum of Square Numbers

2017-07-11 10:59 375 查看
https://leetcode.com/problems/sum-of-square-numbers/#/description

给一个数字c,判断是否满足a2 +
b2 =
c

双指针,end最大为c开根号,beg最小为0.判断当前beg和end是否满足,然后相应移动beg或者end

public class Solution {
public boolean judgeSquareSum(int c) {
int beg = 0;
int end = (int) Math.sqrt(c);
while (beg <= end) {
int target = beg * beg + end * end;
if (target > c) {
end--;
} else if (target < c) {
beg++;
} else {
return true;
}
}
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: