Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 92c80f9 commit 5a52cd9Copy full SHA for 5a52cd9
python/007_Reverse_Integer.py
@@ -1,17 +1,34 @@
1
class Solution:
2
def reverse(self, x):
3
# https://leetcode.com/problems/reverse-integer/
4
- flag = True if x < 0 else False
5
- if flag:
+# flag = True if x < 0 else False
+# if flag:
6
+# x = -x
7
+# x = str(x)[::-1]
8
+
9
10
+# x = "-" + x
11
12
+# value = 2 ** 31
13
+# x = int(x)
14
+# if -value <= x < value:
15
+# return x
16
+# return 0
17
18
+ is_neg = False
19
+ if x < 0:
20
x = -x
- x = str(x)[::-1]
21
+ is_neg = True
22
- x = "-" + x
23
+ res = 0
24
+ while x > 0:
25
+ res *= 10
26
+ res += x % 10
27
+ x //= 10
28
+ if is_neg:
29
+ res = -res
30
- value = 2 ** 31
- x = int(x)
- if -value <= x < value:
- return x
- return 0
-
31
+ if res < -2**31 or res > 2**31-1:
32
+ return 0
33
+ return res
34
0 commit comments