Python with one regex runs within 52ms


  • 1
    K

    0_1473059967748_屏幕快照 2016-09-05 3.18.40 PM.png

    import re
    class Solution(object):
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        digit2 = re.compile("^[\ ]*[+-]?(\d+\.?\d*|\d*\.?\d+)(e[+-]?\d+)?[\ ]*$")
        
        return bool(digit2.match(s))

  • 0

    Awesome!!!!!!


  • 0

    Last five lines are a pretty convoluted way to write return bool(digit2.match(s)).

    And see https://discuss.leetcode.com/topic/57482/beats-100


  • 0
    K

    Thanks StefanPochmann, I am a green hand on writing Python and wasn't so familiar with the recent upgrade of this OJ, maybe several months later I will submit again :). I really appreciate your reply as well as your previous posts, they help a lot to me.


  • 0

    IMHO, using regular expressions to solve this problem defeats the entire purpose of the question. As an interviewer, I do not want to see if you know regex; I want to see if you know how to solve an intricate problem with lots of edge cases.


  • 2
    K

    IMHO too, no means to affend you, I do think using regex is a simple way solve that problem and it's quite an ability to choose the tool. On the other hand, using regex doesn't mean you needn't deal with these edge cases, I did consider the edge cases and at last combined them into one. By the way, regardless the code effciency, as an employer, do you want to have millions of long dirty codes in your program which consider these edge cases or a few lines of clean codes which also consider these edge code? Any way, thank you for replying me and hope that there will be a further discussion.


Log in to reply
 

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