class Solution {
public:
int StrToInt(string str)
{
if(str.empty())
{
return 0;
}
int symbol = 1;
if(str[0] == '-')
{
symbol = -1;
str[0] = '0'; //字符'0',不是0
}
else if(str[0] == '+')
{
symbol = 1;
str[0] = '0';
}
int sum = 0;
for(int i=0;i<str.size();++i)
{
if(str[i] < '0' || str[i] > '9')
{
sum = 0;
break;
}
sum = sum *10 + str[i] - '0';
}
return symbol * sum;
}
};
把字符串转换成整数
最新推荐文章于 2022-07-10 20:43:58 发布