Valid Perfect Square思路:牛顿二分法,注意边界判定。
GitHub地址:https://github.com/corpsepiges/leetcode
public class Solution {
public boolean isPerfectSquare(int num) {
int x=num;
while(true){
int t=(x+num/x)/2;
if (t*t==num||(t-1)*(t-1)==num||(t+1)*(t+1)==num) {
return true;
}
if (x==t||x==t-1||x==t+1) {
return false;
}
x=t;
}
}
}
本文介绍了一种使用牛顿二分法来判断一个整数是否为完全平方数的有效算法,并提供了详细的实现代码。
430

被折叠的 条评论
为什么被折叠?



