Simplest Solution in cpp


#1

int Solution::atoi(const string A) {
int count = 0;
auto it = A.begin();
while(*it == ’ ’ && it!= A.end()){
it++;
}
long long ret = 0;
if(*it == ‘-’){
count = 1;
it++;
}else if(*it == ‘+’){
count = 0;
it++;
}

while(*it != ' ' && it!= A.end()){
    int a = *it - 48;
    
    if(a>= 0 && a<10){
        ret = ret*10 + a;
        if(ret>INT_MAX){
            ret = INT_MAX;
            break;
        }
    }else{
        break;
    }
    it++;
}

if(count == 1){
    if(ret == INT_MAX){
        return INT_MIN;
    }
    ret = ret*(-1);
}

return int(ret);

}