Sum of Square Numbers
Given a non-negative integer c
, decide whether there're two integers a
and b
such that a2 + b2 = c
.
Example 1:
Input: c = 5 Output: true Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: c = 3 Output: false
Solution(Java):
class Solution {
public boolean judgeSquareSum(int c) {
int left=0, right=(int)Math.sqrt(c);
while(left<=right){
if(left*left+right*right==c)
return true;
else if(left*left+right*right<c)
left++;
else
right--;
}
return false;
}
}
Comments
Post a Comment