Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
class Solution {
public:
int myAtoi(string str) {
long long cur=;
int num=,i=;
int flag1=,flag2=;
while(str[i]!='\0' && str[i]==' ') i++;
if(str[i]=='-') flag1++,i++;
else if(str[i]=='+') flag2++,i++;
for(; str[i]!='\0'; i++)
{
if(str[i]>='' && str[i]<='')
{
if(flag1==)
{
cur=cur*-(str[i]-'');
if(cur<-) return -;
}
else if(flag1==) cur=-str[i]+'',flag1++;
else
{
cur=cur*+(str[i]-'');
if(cur>) return ;
}
}
else break;
}
num=(int)cur;
return num;
}
};