Why the input " -0012a42" should output -12?


  • 0
    P

    Input:
    " -0012a42"
    Output:
    0
    Expected:
    -12

    I just think the output should be 0 because there is an invalid world "a" in the string. How can I understand the logic of solving this problem?


  • 0
    K

    Because you should read the string until you get smth like a 'a' ( not digit ).
    So you read '-' , skip two zeros, read '1' and '2' then get 'a' and stop.

    Requirements for atoi:

    The string can contain additional characters after those that form the
    integral number, which are ignored and have no effect on the behavior
    of this function.


Log in to reply
 

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