" -04f" my solution doesn't pass


  • 0
    J

    my solution failed on this test case: " -04f". Can't figure it out. Anyone can help please?

    class Solution {
    public:

    int myAtoi(string str) {
        int sign = 1;
        long long result = 0;
        int p= 0;
        int n = str.size();
        if(n == 0) return 0;
        for (;p<n;p++)
        {
            char t = str[p];
            printf("%c\n", t);
            if(t == ' ')
                p++;
            else if(t == '+'){
                p++;
                break;
            }
            else if(t == '-'){
                printf("%d\n",sign);
                p++;
                sign = -1;
                break;
            }
            else
                break;
        }
        //printf("%d\n",p);
    
        for(; p<n; p++)
        {
            char t = str[p];
            if(t>='0'&&t<='9')
                result=result*10+ t-'0';
            else
                break;
        }
        
        result = result*sign;
        if(result<INT_MIN) return INT_MIN;
        if(result>INT_MAX) return INT_MAX;
        return result;
        
    }
    

    };


Log in to reply
 

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