[leetcode]633. Sum of Square Numbers
Analysis
周五啦~—— [嘻嘻~]
Given a non-negative integer c, your task is to decide whether there’re two integers a and b such that a2 + b2 = c.
不要妄想两个for暴力解决,暴力是不可能AC的,这辈子都不可能,二分查找才是爸爸!
Implement
class Solution {
public:
bool judgeSquareSum(int c) {
int c1 = sqrt(c);
int low = 0;
int high = c1;
while(low <= high){
if(low*low+high*high < c)
low++;
else if(low*low+high*high > c)
high--;
else
return true;
}
return false;
}
};