题目描述
思路分析
字符串模拟题。简单模拟下就好,直接看代码。
代码实现
class Solution {
public:
int StrToInt(string str) {
int res=0;
int k=0;
bool pos=true;
if(str[k]=='-') pos=false,k++;
else if(str[k]=='+') pos=true,k++;
for(int i=k;i<str.size();i++){
if(isdigit(str[i]))
res=res*10+str[i]-'0';
else return 0;
}
if(!pos) res=-res;
return res;
}
};