42 ms 98.71% Python Ez Understand Code


  • 0
    Q

    **This result uses Python standard Number and String functions

    use python build in String[::-1] reverse function,
    There are better ways to do the reversed number overflow checks.
    Hope you can improve it. I am new to python XD.**

    '''
    class Solution(object):
    def reverse(self, x):
    """
    :type x: int
    :rtype: int
    """

        if x > 2147483647 or x < -2147483648 or x == 0:  #overflow and 0 check
            return 0
    
        if x < 0: sign = -1
        else: sign = 1
                
        result = int(str(abs(x))[::-1]) * sign
        
        # Check result overflows
        if result > 2147483647 or result < -2147483648:
            return 0
        else:
            return result
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.