reverse_integer
1# @leet start 2class Solution: 3 def reverse(self, x: int) -> int: 4 """ 5 This function reverse an integer by using modulo on each digit in the 6 and putting it in a variable called res. 7 """ 8 sign = -1 if x < 0 else 1 9 x *= sign 10 11 res = 0 12 while x: 13 res = res * 10 + x % 10 14 x //= 10 15 res *= sign 16 17 if res > 2**31 - 1 or res < -(2**31): 18 return 0 19 20 return res 21 22 23# @leet end 24q = Solution().reverse 25 26 27def test(): 28 assert q(123) == 321 29 assert q(-123) == -321 30 assert q(120) == 21
class
Solution:
3class Solution: 4 def reverse(self, x: int) -> int: 5 """ 6 This function reverse an integer by using modulo on each digit in the 7 and putting it in a variable called res. 8 """ 9 sign = -1 if x < 0 else 1 10 x *= sign 11 12 res = 0 13 while x: 14 res = res * 10 + x % 10 15 x //= 10 16 res *= sign 17 18 if res > 2**31 - 1 or res < -(2**31): 19 return 0 20 21 return res
def
reverse(self, x: int) -> int:
4 def reverse(self, x: int) -> int: 5 """ 6 This function reverse an integer by using modulo on each digit in the 7 and putting it in a variable called res. 8 """ 9 sign = -1 if x < 0 else 1 10 x *= sign 11 12 res = 0 13 while x: 14 res = res * 10 + x % 10 15 x //= 10 16 res *= sign 17 18 if res > 2**31 - 1 or res < -(2**31): 19 return 0 20 21 return res
This function reverse an integer by using modulo on each digit in the and putting it in a variable called res.
def
q(x: int) -> int:
4 def reverse(self, x: int) -> int: 5 """ 6 This function reverse an integer by using modulo on each digit in the 7 and putting it in a variable called res. 8 """ 9 sign = -1 if x < 0 else 1 10 x *= sign 11 12 res = 0 13 while x: 14 res = res * 10 + x % 10 15 x //= 10 16 res *= sign 17 18 if res > 2**31 - 1 or res < -(2**31): 19 return 0 20 21 return res
This function reverse an integer by using modulo on each digit in the and putting it in a variable called res.
def
test():