1.比Q4 簡單,不需要返回整數部分,只需要mid*mid==num即可
2.需要注意返回值是bool形式
1 class Solution { 2 public boolean isPerfectSquare(int num) { 3 int left=0,right=num; 4 while(left<=right){ 5 int mid=(left+right)/2; 6 if((long)mid*mid<num){ 7 left=mid+1; 8 } 9 else if((long)mid*mid>num){ 10 right=mid-1; 11 } 12 else{ 13 return true; 14 } 15 16 } 17 return false; 18 } 19 }