题解 | #把字符串转换成整数#
把字符串转换成整数
https://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e
class Solution { public: int StrToInt(string str) { int sum=0; int flag=1; if(str.size()==0) return 0; if(str[0]=='-') { str[0]='0'; flag=-1; } if(str[0]=='+') { str[0]='0'; flag=1; } for(int i=0;i<str.size();i++) { if(str[i]<'0'||str[i]>'9') return 0; int num=str[i]-'0'; sum=sum*10+num; } return flag*sum; } };