My solution not getting accepted for Reverse integer


  • 0
    V

    When I run locally.
    for this same input 1534236469 I am getting 0 but when its run on leetCode it says its getting output as 9646324351

     def reverse(self, x):
        max = 2147483647
        k=[]
        val = 0
        reminder = mod = abs(x)
        while(mod >= 1 or reminder >= 1):
            mod = reminder % 10
            reminder = int(reminder / 10)
            if(mod != reminder):
                k.append(mod)
        val = ''.join(str(i) for i in k)
        if(x < 0):
            val = "-"+str(val)
        if(val == ''):
            val = 0
        m = int(val)
        if(m > max):
            print(int(0))
        else:
            print(m)
        return m

  • 1

    You're probably getting 9646324351 locally as well. Ignore your debugging prints, the true output and the only thing that matters is the return value.


  • 0
    A

    I got same issue with you.

    Input: 1534236469 Output: 9646324351 Expected: 0

    then i checked the spoilers with this line:

    Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

    then AC

    class Solution:
    # @param {integer} x
    # @return {integer}
    def reverse(self, x):
        flag = False
        if x < 0:
            flag = True
        elif x == 0:
            return 0
        
        min = -2147483648 
        max = 2147483647
        s = str(abs(x))[::-1]
        for i in range(len(s)):
            if s[i] != '0':
                r = 0-int(s[i::]) if flag == True else int(s[i::])
                if r > max or r < min:
                    return 0
                else:
                    return r

Log in to reply
 

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