8. String to Integer (atoi)


Implement atoi to convert a string to an integer.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Firstly, strip the left whitespace, and then judge whether the first character is - or +. And then traverse the string to get the number. IF the number out of range, get the minimum of INT_MAX and value of the maximum of INU_MIN and value.

class Solution(object):
    def myAtoi(self, str):
        :type str: str
        :rtype: int
        str = str.lstrip()
        flags = {'-': -1, '+': 1}
        integer, flag = 0, 1
        if str and str[0] in flags:
            flag = flags[str[0]]
            str = str[1:]
        for c in str:
            if c.isdigit():
                integer = integer * 10 + ord(c) - ord('0')
        integer = flag * integer
        if integer > 0:
            integer = min(2147483647, integer)
            integer = max(-2147483648, integer)
        return integer

Using Regex to do it.


Zero-width Assertion(零宽断言)

  1. (?=exp)     匹配exp前面的位置
  2. (?<=exp)     匹配exp后面的位置
  3. (?!exp)      匹配后面跟的不是exp的位置
  4. (?<!exp)     匹配前面不是exp的位置

This solution is provided by @agave.

Using regex expression ^(?<![\+\-])[\+\-]?[0-9]+ to find the first valid number. Using zero-width assertion (?<\![\+\-]) to avoid the case like +-1.

def ov(n):
    MAX = 2 ** 31 - 1
    MIN = -2 ** 31
    return MIN if n < MIN else\
           MAX if n > MAX else n

class Solution(object):
    def myAtoi(self, str):
        import re
        str = str.strip()
        m = re.findall(r'^(?<![\+\-])[\+\-]?[0-9]+', str)
        if not m or not str:
            return 0

        m, d = m[0], lambda x: ord(x) - 48
        sign = 1 if m[0] != '-' else -1
        res = 0
        for e in m:
            if e not in '+-':
                res = res * 10 + d(e)
        return ov(res *sign)