Sword refers to Offer 67. Convert a string to an integer (C++) Digital out-of-bounds processing

Write a function StrToInt to realize the function of converting a string into an integer. You cannot use atoi or other similar library functions.
First, the function discards useless beginning space characters as needed, until it finds the first non-space character.
When the first non-empty character we find is a positive or negative sign, combine this symbol with as many consecutive digits as possible behind it as the sign of the integer; if the first non-empty character is Number, it is directly combined with the following consecutive number characters to form an integer.
In addition to the valid integer part of the string, there may also be extra characters, these characters can be ignored, they should not affect the function.
Note: If the first non-space character in the string is not a valid integer character, the string is empty, or the string contains only blank characters, your function does not need to be converted.
In any case, if the function cannot perform a valid conversion, please return 0.
Insert picture description here
Example 1:

输入: "42"
输出: 42

Example 2:

输入: "   -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
     我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。

Example 3:

输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。

Example 4:

输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
     因此无法执行有效的转换。

Insert picture description here
Note: This question is the same as the 8 questions on the main site: https://leetcode-cn.com/problems/string-to-integer-atoi/

Problem-solving ideas

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

class Solution {
    
    
public:
    int strToInt(string str) {
    
    
        if(str.empty())  return 0;   //空字符串
        int i = 0;
        while(i < str.size() && str[i] == ' ') i++;//1、首部空格: 删除之即可;跳出while循环的i表示第一个非空字符
        //字符串仅包含空白字符   第一个非空格字符不是一个有效整数字符
        //i == str.size()表示字符串已经遍历完了
        if( i == str.size() || ('A' <= str[i] &&str[i] <= 'Z') || ('a' <= str[i] &&str[i] <= 'z'))  return 0;
        
        int sign = 1;
        if(str[i] == '-' || str[i] == '+')  //判断数字的正负号
        {
    
    
            if(str[i] == '-')   sign *= -1;
            i++;
        }

        int val = 0;
        int bndry = INT_MAX/10;// bndry = 2147483647 / 10 = 214748364
        while(i < str.size() && '0'<=str[i] && str[i]<='9') 
        {
    
    
            if(val > bndry || val == bndry && str[i] > '7')  return sign == 1 ? INT_MAX: INT_MIN;//两种越界
            val = val*10 + (str[i] - '0');//求值res=10×res+x
            i++;//i加一
        }
        return sign*val;//最终值
    }
};

Complexity analysis:

Time complexity O(N): where N is the length of the string, and linear traversal of the string takes O(N) time.
Space complexity O(N): After deleting the leading and trailing spaces, a new string needs to be created. In the worst case, it will occupy O(N) extra space.

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114845612