java 完全平方数
时间: 2023-10-30 07:07:52 浏览: 141
Java逻辑编程:求解完全平方数
要判断一个数是否是完全平方数,可以使用以下方法:
```java
public class PerfectSquare {
public static boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
int left = 0;
int right = num;
while (left <= right) {
long mid = left + (right - left) / 2;
long square = mid * mid;
if (square == num) {
return true;
} else if (square < num) {
left = (int) mid + 1;
} else {
right = (int) mid - 1;
}
}
return false;
}
}
```
阅读全文