sqrtx 发表于 2018-04-23 | 分类于 algorithm , leetcode | 阅读次数 | 12345678910111213141516171819202122232425262728293031323334"""https://leetcode.com/problems/sqrtx/description/"""class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int >>> s = Solution() >>> s.mySqrt(4) 2 >>> s.mySqrt(0) 0 >>> s.mySqrt(8) 2 >>> s.mySqrt(1) 1 >>> s.mySqrt(2147395600) 46340 """ if 0 <= x < 1: return 0 elif x == 1: return 1 else: # how to choose the init value ? x0 = x // 2 while True: # x**2/4 > x if x0**2 <= x: return x0 x0 = int((x0 + x / x0) / 2) Donate WeChat Pay Alipay