(self, x: int)
| 1 | class Solution: |
| 2 | def reverse(self, x: int) -> int: |
| 3 | # Integer.MAX_VALUE = 2147483647 (end with 7) |
| 4 | # Integer.MIN_VALUE = -2147483648 (end with -8 ) |
| 5 | |
| 6 | MIN = -2147483648 # -2^31, |
| 7 | MAX = 2147483647 # 2^31 - 1 |
| 8 | |
| 9 | res = 0 |
| 10 | while x: |
| 11 | digit = int(math.fmod(x, 10)) # (python dumb) -1 % 10 = 9 |
| 12 | x = int(x / 10) # (python dumb) -1 // 10 = -1 |
| 13 | |
| 14 | if res > MAX // 10 or (res == MAX // 10 and digit > MAX % 10): |
| 15 | return 0 |
| 16 | if res < MIN // 10 or (res == MIN // 10 and digit < MIN % 10): |
| 17 | return 0 |
| 18 | res = (res * 10) + digit |
| 19 | |
| 20 | return res |
no outgoing calls
no test coverage detected