In Python, integer has no true fixed maximum, only limited by available memory.
def reverse(self, x): s = str(x) res = int('-' + s[1:][::-1]) if s == '-' else int(s[::-1]) return res if -2147483648 <= res <= 2147483647 else 0
def reverse(self, x): if x>=0: res = int(str(x)[::-1]) else: res = int('-' + str(x)[:0:-1])
Your code looks simple because you have utilized in-built functions in python. Kudos for writing such a simple and brilliant code. But, that doesn't make our program efficient. I believe that it depends on the number of operations such as add, sub, or shift that the program ultimately takes. Hence, the usual solution works better than yours.
Simple answer with 42ms running time
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ if x == 0: return x else: x = str(x) if '-' in x: x=int('-'+''.join(reversed(x[1::]))) else: x = reversed(x) x=int(''.join(x)) if abs(x) > 2**31: return 0 else: return x
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.