Topic:
Implement atoi to convert a string to an integer.
Hint:carefully consider all possible input cases. If you want a challenge, please don't see below and ask yourself what is the possible input cases.
Notes:it is intended-problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front. is to change the string to an integer, note the ' + ' and the space, and the overflow condition
Code:
Class Solution {public
:
int myatoi (string str) {
int n = str.size ();
int p = 0;
int z = 1;
A long long temp = 0;
while (str[p] = = ")
p++;
if (str[p] = = ' + ' | | str[p] = = '-')
{
if (str[p] = = '-')
z =-1;
p++;
}
for (int i = p; I < N;i + +)
{
if (str[i] >= ' 0 ' && str[i] <= ' 9 ')
{
temp = temp*10 + St r[i]-' 0 ';
if (Z * Temp < Int_min)
return int_min;
if (Z * temp > Int_max)
return int_max;
}
else break
;
}
return z * TEMP;
}
;