GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-1/python/069_Sqrt(x).py at master · dolphintear/leetcode-1 Expand file tree/ Copy path069_Sqrt(x).pyMore file actions 35 lines (34 loc) · 958 Bytes / Copy path069_Sqrt(x).pyFile metadata and controls35 lines (34 loc) · 958 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 class Solution: # def mySqrt(self, x): # """ # :type x: int # :rtype: int # """ # if x == 0: # return 0 # low = 0 # high = x # last = x # while high >= low: # mid = (low + high) / 2 # temp = mid * mid # if temp == x: # return mid # elif temp < x: # low = mid + 1 # last = mid # else: # high = mid - 1 # return last def mySqrt(self, x): # sqrt(x) = 2 * sqrt(x / 4) for n % 4 == 0 # sqrt(x) = 1 + 2 * sqrt(x / 4) for n % 4 != 0 if x == 0: return 0 if x < 4: return 1 res = 2 * self.mySqrt(x / 4) # (res + 1) * (res + 1) >= 0 for avoiding overflow if (res + 1) * (res + 1) <= x and (res + 1) * (res + 1) >= 0: return res + 1 return res |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |