Python solution


  • 1
    K
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        str = str.strip()
        for i,v in enumerate(str):
            if v not in '+-' and not v.isdigit():
                str = str[:i]
                break
        try:
            ret = int(str)
        except:
            return 0
        return max(min(ret, 2**31-1), -2**31)

Log in to reply
 

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