class Solution {
public:
bool isPerfectSquare(int num) {
if(num == 1){return true;}
int low = 1;
int high = num / 2;
while(low <= high){
long mid = (low + high) / 2;
if(mid * mid == num){return true;}
else if(mid * mid < num){
low = mid + 1;
}
else if(mid * mid > num){
high = mid - 1;
}
}
return false;
}
};
367. Valid Perfect Square
Easy
42597FavoriteShare
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 Output: true
Example 2:
Input: 14 Output: false