mirror of
https://github.com/SunnyQjm/algorithm-review.git
synced 2026-06-03 08:16:43 +08:00
update
This commit is contained in:
+2
-2
@@ -2,12 +2,12 @@
|
||||
# coding=utf-8
|
||||
|
||||
|
||||
################################################################3
|
||||
################################################################
|
||||
# LeetCode: 69 Sqrt(x)
|
||||
# Implement int sqrt(int x).
|
||||
# Compute and return the square root of x, where x is guaranteedto be a non-negative integer.
|
||||
# Since the return type is an integer, the decimal digits aretruncated and only the integer part of the result is returned.
|
||||
################################################################3
|
||||
################################################################
|
||||
|
||||
class Solution:
|
||||
def mySqrt(self, x):
|
||||
|
||||
@@ -36,12 +36,12 @@ class Solution:
|
||||
3. 将|x|从个位开始向左遍历,依次叠加到结果里面;
|
||||
4. 最后判断结果是否溢出(与32位有符号整形的最大值和最小值进行比对)
|
||||
"""
|
||||
isNegitive = -1 if x < 0 else 1
|
||||
isNegative = -1 if x < 0 else 1
|
||||
result, x = 0, abs(x)
|
||||
while x > 0:
|
||||
result = result * 10 + x % 10
|
||||
x = x // 10
|
||||
result = isNegitive * result
|
||||
result = isNegative * result
|
||||
|
||||
return 0 if result > INT_MAX_VAL or result < INT_MIN_VAL else result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user