Simple python solution based on regex


  • 1
    L
    import re
    
    class Solution(object):
    
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
            try:
                g = re.match(r' *([+-]?\d+).*', str)
                ret = int(g.group(1))
                return min(max(ret, -2147483648), 2147483647)
            except:
                return 0

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.