LeetCode字符串转换为整型C++版

C++代码如下:

class Solution {
public:
    int myAtoi(string str) {
        int  sign = 0;
        long result = 0;
        for (int i = 0;i<str.size();i++) {
            if(fu==0){
                if (str[i] == '-') sign = -1;
                else if (str[i] == '+') sign = 1;
                else if (str[i] == ' ') continue;
                else if (str[i] <= '9' && str[i] >= '0') {
                    result=str[i] - '0';
                    sign=1;
                }
                else return 0;
            }
            else{
                if (str[i] <= '9' && str[i] >= '0'){
                    result = result * 10 +str[i] - '0';
                    if (result*fu > INT_MAX)  return INT_MAX;
                    if (result*fu < INT_MIN) return INT_MIN;
                }
                else return result*sign;
            }
        }
            return result*sign;
    }
};

你可能感兴趣的:(c++)