-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41fda2c
commit 0f1fcfe
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
#@author: rye | ||
#@time: 2019/3/19 | ||
|
||
''' | ||
二分法。 | ||
当时没想到。 | ||
tips: | ||
注意return的条件 | ||
''' | ||
class Solution(object): | ||
def mySqrt(self, x): | ||
""" | ||
:type x: int | ||
:rtype: int | ||
""" | ||
# 作弊 | ||
# return int(x ** (1 / 2)) | ||
|
||
if x == 0: | ||
return 0 | ||
if x == 1: | ||
return 1 | ||
l, r = 0, x - 1 | ||
while l <= r: | ||
# mid1 = l + (r - l) >> 1 | ||
mid = l + ((r - l) >> 1) | ||
if mid ** 2 <= x and (mid + 1) ** 2 > x: | ||
return mid | ||
elif mid ** 2 > x: | ||
r = mid - 1 | ||
else: | ||
l = mid + 1 | ||
|
||
if __name__ == '__main__': | ||
x = 3 | ||
print(Solution().mySqrt(x)) |