Question: https://oj.leetcode.com/problems/sqrtx/
Question Name: Sqrt(x)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution: # @param x, an integer # @return an integer def sqrt(self, x): begin, end = 0, x # Use binary search to find the largest # integer i such that, i * i <= x while begin <= end: mid = (begin + end) // 2 temp = mid * mid if temp == x: return mid elif temp < x: begin = mid + 1 else: end = mid - 1 return end |