GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/069_Sqrt(x).py at master · tairen99/leetcode

Expand file tree

/

069_Sqrt(x).py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
35 lines (34 loc) · 958 Bytes
/

069_Sqrt(x).py

Copy path

File metadata and controls

35 lines (34 loc) · 958 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.