C++简单代码/10行:

class Solution {
public:
    int StrToInt(string str) {
        int flag = 1, i = 0;
        long res = 0;
        if (str[i] == '-') flag = -1;
        if (str[i] == '+' || str[i] == '-') i ++;
        for (; i < str.size(); i ++) {
            if (!isdigit(str[i])) return 0;
            res = res * 10 + (str[i] - '0');
        }
        return flag * res;
    }
};