We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 29ebdc1 + 8eb6ff2 commit 9d2d5a1Copy full SHA for 9d2d5a1
reverse32bitsignedint.py
@@ -0,0 +1,19 @@
1
+# reverse a 32 bit signed int
2
+# if ans overflows then returns 0
3
+def reverse32bitsignedint(n):
4
+ is_negative = 0
5
+ if n < 0:
6
+ is_negative = 1
7
+ n = -n
8
+ ans = 0
9
+ while n:
10
+ quotient = n // 10
11
+ remainder = n % 10
12
+ n = quotient
13
+ ans = ans * 10 + remainder
14
+ limit = 1 << 31
15
+ if is_negative:
16
+ ans = -ans
17
+ if (ans <= -limit) or (ans >= limit - 1):
18
+ return 0
19
+ return ans
0 commit comments