AcWing 87. 把字符串转换成整数
原题链接
中等
作者:
术
,
2020-12-22 19:45:20
,
所有人可见
,
阅读 333
class Solution
{
public:
int strToInt(string str)
{
bool is_zheng=true;
int i;
while(str[i]==' ')
i++;
if(str[i]=='-')
{
is_zheng=false;
i++;
}
else if(str[i]=='+')
i++;
int cnt=0;
int number=0;
bool is_yue=false;//也可以用long long
while(i<str.size()&&str[i]>='0'&&str[i]<='9'){
if(number<0) {is_yue=true;break;}//
number=number*10+str[i]-'0';
//cout<<str[i];
i++;
}
//cout<<is_yue<<endl;
cout<<INT_MIN<<endl;
if(is_yue&&is_zheng) number=INT_MAX;
else if(is_yue&&is_zheng==false) number=INT_MIN;
if(is_zheng==false) number*=-1;
return number;
}
};