其他分享
首页 > 其他分享> > 69. Sqrt(x)

69. Sqrt(x)

作者:互联网

Using binary search, time complexity: O(log(x))

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

 

标签:binary,search,int,mid,Sqrt,Solution,Using,69
来源: https://www.cnblogs.com/feiflytech/p/16079969.html