367. Valid Perfect Square - Easy
前往題目
想法
- 經典的
Binary Search
思路
- 經典
Binary Search
Code
class Solution {
public boolean isPerfectSquare(int num) {
int l = 1, r = num;
while (l <= r) {
int mid = l + (r - l) / 2;
long tmpSquare = (long) mid * (long) mid;
if (tmpSquare > num) {
r = mid - 1;
} else if (tmpSquare < num) {
l = mid + 1;
} else {
return true;
}
}
return false;
}
}
367. Valid Perfect Square - Easy
https://f88083.github.io/2024/09/27/367-Valid-Perfect-Square-Easy/