package LeetCode.contact;
public class leetcode_367 {
public static void main(String[] args) {
int num = 4;
Solution_367 sol = new Solution_367();
System.out.println(sol.isPerfectSquare(num));
}
}
class Solution_367 {
public boolean isPerfectSquare(int num) {
int low =1;
int high = num; //[low,right]闭区间
while(low <= high){ //当low==right,区间[left,right]依然有效。
int mid = (low +high)/2;
double t = ((double)num)/mid; //num除以中间的数mid得到t,如果t等于mid
if(t == mid){
return true;
}else if(t < mid){
high = mid - 1;
}else{
low = mid + 1;
}
}
return false;
}
}
LeetCode_367:有效的完全平方数:Java
最新推荐文章于 2024-03-30 09:00:20 发布