# Time:  O(logn)
# Space: O(1)
#
# Implement int sqrt(int x).
# 
# Compute and return the square root of x.
#

class Solution:
    # @param x, an integer
    # @return an integer
    def sqrt(self, x):
        if x < 2:
            return x
        
        low, high = 1, x / 2
        while high >= low:
            mid = low + (high - low) / 2
            if x / mid < mid:
                high = mid - 1
            else:
                low = mid + 1
        return high

if __name__ == "__main__":
    print Solution().sqrt(10)
            
# Time:  O(logn)
# Space: O(1)

# Implement int sqrt(int x).
# 
# Compute and return the square root of x.

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

        return left - 1


if __name__ == "__main__":
    print Solution().sqrt(10)
            

Update sqrtx.py