Python solution


  • 0
    P

    My Python code, it got accetped.

    class Solution(object):
        def isNumber(self, s):
            """
            :type s: str
            :rtype: bool
            """
            try:
                isinstance(float(s), float)
                if True:
                    return True
                else:
                    return False
            except ValueError:
                return False
    

  • 0
    N

    Feels a bit "cheaty", to be honest. I see the purpose of this task to implement the checking function yourself, not to ask interpreter to try and do that for you. This would be superior version for production, though.

    BTW, your code can be made shorter:

    try:
       float(s)
       return True
    except ValueError:
       return False
    

    This would be accepted as well.


Log in to reply
 

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