solution using python re


  • 0
    D

    You need consider following cases:

    1. invalid input
    2. leading whitespaces
    3. overflow
    class Solution(object):
        
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
            import re
            p = re.compile(r'^[+-]?[0-9]+')
            m = re.match(p, str.strip())
            if m:
                ret = int(m.group())
                if ret < -0x80000000:
                    return -0x80000000
                elif ret > 0x7fffffff:
                    return 0x7fffffff
                return ret
            else:
                return 0
    

Log in to reply
 

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