题目:
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt
.
Example 1:
Input: 16 Returns: True
Example 2:
Input: 14 Returns: False
思路:
二分查找即可。
程序:
class Solution {
public:
bool isPerfectSquare(int num) {
if(num < 1) return false;
if(num == 1) return true;
int left = 0, right = num / 2;
while(left <= right)
{
long mid = (left+right)/2;
long val = mid*mid;
if(val == num) return true;
else if(val > num) right = mid-1;
else left = mid+1;
}
return false;
}
};