0%

LeetCode: 367. Valid Perfect Square

直接二分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
bool isPerfectSquare(int num) {
long long beg=1, end=num;
while(beg<=end)
{
long long mid = (end-beg)/2+beg;
auto t = mid*mid;
if(t==num)
return true;
else if(t<num)
{
beg=mid+1;
}
else
{
end=mid-1;
}
}
return false;
}
};