LeetCode-Java题解 367. Valid Perfect Square
作者:互联网
题目地址:367. Valid Perfect Square
解题思路:有了上一道题69. Sqrt(x)的洗礼,这道题我只能说,没有意思!
除了需要注意1
这个特殊值,其他的基本和上一道题目如出一辙,甚至比上一题简单很多,直接二分查找,所以别多说,一把梭。
class Solution {
public boolean isPerfectSquare(int num) {
if (num==1) {
return true;
}
int low = 1;
int high = num/2;
while (low <= high) {
int mid = low + (high - low) / 2;
if ((long)mid * mid == num) {
return true;
} else if ((long)mid * mid > num) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return false;
}
}
标签:Perfect,Square,int,题解,mid,high,num,low 来源: https://www.cnblogs.com/hooyeefam/p/15827175.html