LeetCode-- primary algorithm - string integer conversion

String integer conversion (atoi)

Atoi you to implement a function, it can convert a string to an integer.

First, the function will begin with a space character discard useless if necessary, until the find to the first non-space character so far.

When we find the first non-space character is a positive or negative number, the combination of the symbols as much as possible with consecutive numbers up later, as the sign of integer; if the first non-space character is figures, which directly after the continuous numeric characters are combined to form an integer.

In addition to the string after a valid integer part may also exist extra characters, these characters can be ignored, they should not affect a function.

Note: if the character string in the first non-space character is not a valid integer character string is empty or contains only white space character string, then you will not need to be a function of conversion.

In any case, if the function can not effectively convert, 0 is returned.

Description:
We assume that the size of the environment can store 32-bit signed integer, then the value range of [-2 ^ 31, 2 ^ 31 - 1] If the value exceeds this range, qing return INT_MAX (2 ^ 31 - 1) or INT_MIN (-2 ^ 31).

Example 1:

Input: "42"
Output: 42

Example Two:

Input: "-42"
Output: -42
Explanation: a first non-blank character '-', it is a negative sign.
We will all digital consecutive negative number and later combined as much as possible, and finally get -42.

Example Three:

Input: "4193 with words"
Output: 4193
Explanation: converting the digital OFF '3', because the next character is not numeric.

Example Four:

Input: "words and 987"
Output: 0
Explanation: a first non-blank character 'w', but it is not a positive number or negative number.
Therefore, the conversion can not be performed effectively.

Example five:

Input: "-91283472332"
Output: -2147483648
explanation: the number "-91283472332" Over the range of 32-bit signed integer.
Accordingly return INT_MIN (-2 ^ 31).

Algorithms ideas:

The above-described character string entered slowly filtration conditions, ultimately to give a string of numeric strings strNum, wherein the orientation of the string 10 within. (2 ^ 31 = 2,147,483,647), then either output.

  1. First, determine the null character, directly back to 0.
if(str.length() < 1)
  1. Filtration beginning character is empty, get new strings.
int iNullStrCnt = 0;
while(iNullStrCnt < str.length() && str[iNullStrCnt] == ' '){
    iNullStrCnt++;
}
str = str.substr(iNullStrCnt,str.length());
  1. Determining a first non-blank character, or if it is not a digital '+', '-', the process directly returns 0
    if yes, place '+', '-', at the beginning of the symbol to obtain a digital string.
if(str[0] < '0' || str[0] > '9'){
	if(str[0] == '-'){
	    bSybolFlag = -1;
	}
	else if(str[0] == '+'){
	    bSybolFlag = 1;
	}
	else{
	    return 0;
	}
	str = str.substr(1,str.length());
  1. Write function to get the string of numbers. Requirements: Get number of consecutive, when there is non-numeric, it represents the end of the data, returns the number of consecutive preceding string. Such as: 123a123 - 123 returns.
    string getNumStr(string str){

        string strNum = "";
        bool bFlag = true;
        for(int i = 0; i < str.length(); i++)
        {
            if(str[i] >= '0' && str[i] <= '9'){
                //跳过,开始字符为0
                if(bFlag && str[i] == '0')
                {
                    continue;
                }
                bFlag = false;
                strNum.push_back(str[i]);
            }
            else{
                break;//非数字,跳出本次循环
            }
        }
        return strNum;
    }
  1. After obtaining the number string, and accordingly beyond the process are:
    (1) numeric string more than 10, 0 is returned.
    (2) The calculated result exceeds the maximum value of return INT_MAX INT_MAX.
    (3) The calculated result is smaller than the maximum value of return INT_MIN INT_MIN.

Code:

class Solution {
public:
    int myAtoi(string str) {
        //空字符 直接返回0
        if(str.length() < 1)
            return 0;
        
        //过滤开头字符为空
        int iNullStrCnt = 0;
        while(iNullStrCnt < str.length() && str[iNullStrCnt] == ' '){
            iNullStrCnt++;
        }
        str = str.substr(iNullStrCnt,str.length());
        
        cout << "去除空格字符str ="<<str<<endl;
        
        //判断第一个非空字符的符号
        int bSybolFlag = 1;
        
        string strNum = "";
        //判断第一个字符是否为‘+’、‘-’,数字,否则直接返回0
        //开头字符非数字的情况
        if(str[0] < '0' || str[0] > '9'){
            if(str[0] == '-'){
                bSybolFlag = -1;
            }
            else if(str[0] == '+'){
                bSybolFlag = 1;
            }
            //非‘+’ 非‘-’ 直接返回0
            else{
                cout<<"非‘+’ 非‘-’ 直接返回0"<<endl;
                return 0;
            }
            str = str.substr(1,str.length());
            cout << "去除符号字符str ="<<str<<endl;
            strNum = getNumStr(str);
        }
        else{
            strNum = getNumStr(str);
            
        }
        cout << "得到数字字符串str ="<<strNum<<"bsybolflag = "<<bSybolFlag<<"最大值="<<INT_MAX<<"最小值 = "<<INT_MIN<<endl;
        //数据超出long的情况直接丢掉
        if(strNum.length()>10){
            if(bSybolFlag == 1){
                return INT_MAX;
            }
            else if(bSybolFlag == -1){
                return INT_MIN;
            }
                
        }
        
        //结果
        long lResultNum = 0;
        for(int i = 0;i < strNum.length(); i++)
        {
            lResultNum = lResultNum*10 + strNum[i] - '0';
            if(lResultNum*bSybolFlag < INT_MIN) return INT_MIN;
            if(lResultNum*bSybolFlag > INT_MAX) return INT_MAX;
        }
        
        return lResultNum*bSybolFlag;
    
    }
    
    string getNumStr(string str){

        string strNum = "";
        bool bFlag = true;
        for(int i = 0; i < str.length(); i++)
        {
            if(str[i] >= '0' && str[i] <= '9'){
                //跳过,开始字符为0
                if(bFlag && str[i] == '0')
                {
                    continue;
                }
                bFlag = false;
                strNum.push_back(str[i]);
            }
            else{
                break;//非数字,跳出本次循环
            }
        }
        return strNum;
    }
};
Published 61 original articles · won praise 89 · Views 200,000 +

Guess you like

Origin blog.csdn.net/qq_33559992/article/details/87879278