Python solution--68ms

  • 0
    class Solution(object):
        def myAtoi(self, sstr):
            :type str: str
            :rtype: int
            # find the valid integral sequence
            inputs = sstr.strip(' ')
            valid_str =  ['-', '+']
            tmp_str = ''
            for i, item in enumerate(inputs):
                if item not in valid_str and not item.isdigit():
                tmp_str += item
            # process exception
                int_str = int(tmp_str)
                return max(-2**31, min(int_str, 2**31-1))
            except Exception:
                return 0

    I just process the invalid value for int() by code block try...exception, 68ms

Log in to reply

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