Correct answer is judged wrong?


  • 0
    G

    Here is the message

    Submission Result: Wrong Answer
    Input: "2147483648"
    Output: 2147483648
    Expected: 2147483647

    My code is

    class Solution:
    # @return an integer
    def atoi(self, str):
        str=str.strip()
        if not str:
            return 0
        sign=1
        
        if str[0]=='-':
            sign=-1
            str=str[1:]
        elif str[0]=='+':
            str=str[1:]
        ans=0
        for d in str:
            if not d.isdigit():
                return ans*sign
            ans=ans*10+ord(d)-ord('0')
        return ans*sign

  • 0
    M

    It's in the spoilers.
    "If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned."

    The true value is greater than INT_MAX, so it returns INT_MAX instead of the actual value.


  • 0
    G

    that's an issue with Python


  • 0
    M

    Yeah, but the test values are based on c++ and java, so 2147483647 is the largest number possible. Just add a couple if statements to the end and it will work.


Log in to reply
 

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