my 9ms solution with C++


  • 0
    L
    int myAtoi(string str) {
        int len = str.size();
        if(len==0) return 0;
        int i = 0,cnt = 0,ispos = 1,tmp = 0;
        while(str[i]==' '&&i!=len) i++;
        if(str[i]=='+'||str[i]=='-'){ispos = str[i]=='+'?1:-1; i++;}
        while(str[i]>='0'&&str[i]<='9'&&i!=len){
            tmp = cnt;
            cnt = cnt*10+(str[i]-'0');
            if(cnt/10!=tmp){
                return ispos==1?INT_MAX:INT_MIN;
            }
            i++;
        }
        return cnt*ispos;
    }
    

Log in to reply
 

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