Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 662 Bytes

8.md

File metadata and controls

40 lines (29 loc) · 662 Bytes

String to Integer (atoi)

Description

link


Solution

see Code


Code

Complexity T : O(n) M : O(n)

class Solution:
    def myAtoi(self, s):
        """
        :type str: str
        :rtype: int
        """
        import re
        INT_MAX = 2**31 - 1
        INT_MIN = -2**31
        res = 0
        
        pattern = r'( *[+-]?[0-9]+)'
        
        if re.match(pattern, s):
            res = int(re.match(pattern, s).group())
        if res > INT_MAX:
            return INT_MAX
        if res < INT_MIN:
            return INT_MIN
        return res