Wrong Answer input:1 output:0


  • 0
    J

    class Solution {
    public:
    int myAtoi(string str) {

        int i;
        long result=0;
        bool flag=true;
        while(str[i]== ' ')
    		i++;
        if (i==str.size())
           return 0;
        if(str[i]=='-')
        { 
            flag=false;
            i++;
        }
        else i++;
        for(i;i<str.size();i++)
        {
            if((str[i])>='0'&&str[i]<='9')
            {
            
                result=result*10+(str[i]-'0');
                if(flag && result>INT_MAX) return INT_MAX;
                if(!flag && -result<INT_MIN)  return INT_MIN;
            }
            else break;
        }
        if(flag)result=-result;
        return (int)result;
           
    }
    

    };


Log in to reply
 

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