69. x 的平方根(java)

链接:https://leetcode-cn.com/problems/sqrtx/

class Solution {
    public int mySqrt(int x) {
        //二分法
      int l =1;
      int r=x;
      int mid =(l+r)/2;
      while(l<=r){
          if(mid==x/mid){
              return mid;
          }
          if(mid>x/mid){
              r=mid-1;
          }
          else{
              l=mid+1;
          }
          mid=(l+r)/2;
      }
      return mid;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37244548/article/details/106800689