Leetcode 367. Valid Perfect Square
题目
Input: 16
Output: trueInput: 14
Output: False思路
解答
class Solution {
public boolean isPerfectSquare(int num) {
int low = 1, high = num;
while (low <= high) {
long mid = (low + high) / 2;
if (mid * mid == num) {
return true;
} else if (mid * mid < num) {
low = (int) mid + 1;
} else {
high = (int) mid - 1;
}
}
return false;
}
}Complexity Analysis
拓展
总结
Reference
Last updated