My 8ms solution


  • 0
    M
    int myAtoi(string str){
    int len=str.length(),Sign=1;
    if(!len)return 0;
    int st=str.find_first_not_of(' ');
    long long sum=0;
    int i=st;
    if(str[i]=='-'){Sign=-1;i++;}
    else if(str[i]=='+')i++;
    for(;i<len&&str[i]<='9'&&str[i]>='0';i++){
    	sum=10*sum+str[i]-'0';
    	if(Sign*sum>INT_MAX)return INT_MAX;
    	if(Sign*sum<INT_MIN)return INT_MIN;
    }
    return int(Sign*sum);
    

    }


Log in to reply
 

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