Sum of Square Numbers
Thoughts
Code
class Solution {
public boolean judgeSquareSum(int c) {
if (c < 0) {
return false;
}
int l = 0, r = (int)Math.sqrt(c);
while (l <= r) {
int cur = (int)Math.pow(l, 2) + (int)Math.pow(r, 2);
if (cur < c) {
l++;
} else if (cur > c) {
r--;
} else {
return true;
}
}
return false;
}
}Analysis
Last updated