leetcode brushing notes 08 string to integer (atoi)

Topic description

Implement  atoi, convert string to integer.

The space character needs to be removed from the string before the first non-blank character is found. If the first non-blank character is a positive sign or a negative sign, select that sign and combine it with as many consecutive digits as possible. This part of the character is the value of the integer. If the first non-blank character is a digit, it is directly combined with subsequent consecutive digit characters to form an integer.

Strings can include extra characters after the characters that form the integer, these characters can be ignored, they have no effect on the function.

No conversion occurs when the first sequence of non-empty characters in the string is not a valid integer; or when the string is empty; or when the string contains only whitespace characters.

Returns 0 if the function cannot perform a valid conversion.

illustrate:

Suppose that our environment can only store 32-bit signed integers with values ​​in the range [−231, 231 − 1]. Returns INT_MAX (231 − 1) or INT_MIN (−231) if the value exceeds the representable range.

Example

Example 1:

Input: " 42 " 
Output: 42

Example 2:

Input: "    -42 " 
Output: - 42 
Explanation: The first non-whitespace character is ' - ' , which is a minus sign.
     We try to combine the minus sign with all consecutive numbers that follow, and we end up with -42 .

Example 3:

Input: " 4193 with words " 
Output: 4193 
Explanation: Conversion ends at digit ' 3 ' because its next character is not a digit.

Example 4:

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

Example 5:

Input: " -91283472332 " 
Output: - 2147483648 
Explanation: The number " -91283472332 " exceeds the 32 -bit signed integer range.
     So INT_MIN (− 231 ) is returned.

topic analysis

Common algorithm interview questions, although the leetcode difficulty label is medium, it seems that the pass rate is not very high, about 14%. In the process of doing this question, you need to pay attention to the following points

1. The '-' sign before negative numbers and the '+' sign before positive numbers

2. Wrong input, such as entering multiple spaces at the beginning, or adding letters after numbers, such as: 898word

3. Consider the problem of numerical overflow, max_int=0x7fffffff min_int=0x80000000

 

answer code

C++ version:

class Solution {
public:
    int myAtoi(string str) {
        const int maxint=0x7fffffff;
        const int minint=0x80000000;
        const long long max=0x100000000;
        long long ans=0;
        bool flag=false;
        int st=0;
        while(st<str.length()&&str[st]==' '){
            st++;
        }
        if(st<str.length()&&str[st]=='+'){
            st++;
        }else{
            if(st<str.length()&&str[st]=='-'){
                flag=true;
                st++;
            }
        }
        for(int i=st;i<str.length();i++){
            if(str[i]<='9'&&str[i]>='0'){
                years = years* 10 +str[i]- ' 0 ' ;
                if (ans>maxint) ans= max;
            }else{
                break;
            }
        }
        if (flag) ans=- ans;
        if (ans>maxint) ans= maxint;
        if (ans<minint) ans= minint;
        
        return ans;
        
    }
};
code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325127368&siteId=291194637
Recommended