美文网首页
69. Sqrt(x)

69. Sqrt(x)

作者: larrymusk | 来源:发表于2017-12-02 18:06 被阅读0次
    int mySqrt(int x) {
            if(x <= 0) return x;  
              
            // Use binary search to find square root  
            int left = 1;  
            int right = x / 2 + 1;  
            int mid = left + (right-left) / 2;  
            while(left +1 < right){  
                mid = left + (right-left) / 2;  
                if(x / mid == mid) return mid;  
                else if(x / mid > mid) left = mid;  
                else right = mid;  
            }
            if(right/mid == mid)
                return right;
            return left;  
        
    }

    相关文章

      网友评论

          本文标题:69. Sqrt(x)

          本文链接:https://www.haomeiwen.com/subject/keanbxtx.html