Easy cpp with simultaneous overflow check


#1

int Solution::atoi(const string A) {
int i=0,sign = 1;
while(A[i]==’ ‘)
i++;
long int ans = 0;
if(A[i]==’+’)
i++;
else if(A[i]==’-’)
{
sign = -1;
i++;
}
while(i<A.size() &&A[i]>=‘0’ && A[i]<=‘9’)
{
ans = 10 * ans + A[i++]-‘0’;
if(anssign >= INT_MAX)
return INT_MAX;
else if(ans
sign<=INT_MIN)
return INT_MIN;
}
ans *= sign;
return ans;
}
`