"Sword Finger Offer" Brush Question Series-(sixty-six) convert a string into an integer

topic

Write a function StrToInt to convert 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-blank character we find is a positive or negative sign, combine the sign with as many consecutive numbers as possible after it, as the sign of the integer; if the first non-blank 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.

Assuming that our environment can only store 32-bit signed integers, the value range is [−231, 231 − 1]. If the value exceeds this range, please return INT_MAX (231 − 1) or INT_MIN (−231).

Ideas

The method of converting a character string into an integer:
1. Iterate from one character of the character string, and convert the current character into a number.
2. Concatenate all the numbers into an integer.

The method of converting characters to numbers: subtract the ASCII code value of the number 0 from the ASCII code value of the current character. The ord() function is used to find the ASCII code value.

Several special cases need to be handled:
1. When there is a space at the beginning of the string, delete the space first.
2. When the string is empty, return 0
3. When the first non-blank character is "+" or "-", save it Sign bit
4. In the process of traversing the string, when the first non-digit character is encountered, it will directly return
5. When the currently saved result value is not within the range of [−2 31 , 2 31 − 1], it will return INT_MAX (2 31 − 1) or INT_MIN (−2 31 )

Code

class Solution:
    def strToInt(self, str: str) -> int:
        s = str.strip()
        if not s: return 0
        number = 0
        start = 0
        flag = 1
        
        if s[0]=='-':
            flag = -1
            start = 1
        if s[0]=='+':
            start = 1
        
        for i in range(start,len(s)):
            if s[i]>='0' and s[i]<='9':
                x = ord(s[i])-ord('0')
                number = number*10+x
                if flag*number>=(2**31-1): return 2**31-1
                if flag*number<=(-2**31): return -2**31
            else:
                break
            
        return flag*number
                

the complexity

Time complexity O(N): where N is the string length, 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 takes O(N) extra space.

Guess you like

Origin blog.csdn.net/weixin_44776894/article/details/107575794