LeetCode Binary Search Summary 二分搜索法小结

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x ==0: return 0
        left = 1 
        right = x/2
        while True:
            mid = left + (right-left)/2
            if x/mid<mid:
                right = mid -1
            else:
                if x/(mid+1) < mid+1:
                    return mid
                left = mid+1
class Solution {
public:
    int mySqrt(int x) {
        if (x <= 1) return x;
        int left = 0, right = x;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (x / mid >= mid) left = mid + 1;
            else right = mid;
        }
        return right - 1;
    }
};