Skip to content

Latest commit

 

History

History
251 lines (160 loc) · 5.29 KB

0065._Valid_Number.md

File metadata and controls

251 lines (160 loc) · 5.29 KB

65. Valid Number

难度: Hard

刷题内容

原题连接

内容描述

Validate if a given string can be interpreted as a decimal number.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

Numbers 0-9
Exponent - "e"
Positive/negative sign - "+"/"-"
Decimal point - "."
Of course, the context of these characters also matters in the input.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

解题方案

思路 1 - 时间复杂度: O(N)- 空间复杂度: O(1)******

首先有一个问题,What is the e notation regarding to decimal numbers?

答:It's another way to express scientific notation; the numbers before E represent the front factor, and the numbers after represent the power (base 10). So, if you see aEb = a * 10 ^ b.

一上来就用最暴力的方式,一种一种case考虑

beats 100%

class Solution:
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s = s.strip()
        if not s:
            return False
        
        dotFlag = eFlag = hasDigit = hasSign = False
        for i in range(len(s)):
            if s[i].isdigit():
                hasDigit = hasSign = True
            elif not dotFlag and s[i]=='.':
                dotFlag = hasSign = True
            elif hasDigit and not eFlag and s[i] == 'e':
                dotFlag = eFlag = True
                hasDigit = hasSign = False
            elif not hasDigit and not hasSign and s[i] in '+-':
                hasSign = True
            else:
                return False
        
        return True if hasDigit else False

思路 2 - 时间复杂度: O(N)- 空间复杂度: O(1)******

参考一下大神aynamron的DFA解法

q1就是我们的起始位置,开始遇到空格的话我们还一直待在q1状态就可以,然后分'+-', digit和'.'分别移动到状态q2, q3和q4, 同样的这样到最后我们会到状态3,5,8或者9,这4种状态我们都算合法,比如

"0" => true
" 0.1 " => true
"2e10" => true
" -90e3   " => true
" 6e-1" => true
"53.5e93" => true

我们发现最终合法的number 一共有4种形式,然后所有这些形式之间都是可以互相转换的

beats 47.10%

class Solution:
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        #define a DFA
        state = [{}, 
              {'blank': 1, 'sign': 2, 'digit':3, '.':4}, 
              {'digit':3, '.':4},
              {'digit':3, '.':5, 'e':6, 'blank':9},
              {'digit':5},
              {'digit':5, 'e':6, 'blank':9},
              {'sign':7, 'digit':8},
              {'digit':8},
              {'digit':8, 'blank':9},
              {'blank':9}]
        currentState = 1
        for c in s:
            if c >= '0' and c <= '9':
                c = 'digit'
            if c == ' ':
                c = 'blank'
            if c in ['+', '-']:
                c = 'sign'
            if c not in state[currentState].keys():
                return False
            currentState = state[currentState][c]
        if currentState not in [3,5,8,9]:
            return False
        return True

思路 3 - 时间复杂度: O(N)- 空间复杂度: O(1)******

正则表达式解决, 参考lime66

  • The integer RE is: ^[+-]?\d+$
  • The float RE is: ^[+-]?((\d*.\d+)|(\d+.\d*))$
  • The scientific notation RE is: ^[+-]?((\d*.\d+)|(\d+(.\d*)?))[eE][+-]?\d+$
  • We can combine them as one: ^[+-]?((\d*.\d+)|(\d+(.\d*)?))([eE][+-]?\d+)?$

beats 100%

class Solution:
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        regex = re.compile(r'^[+-]?((\d*\.\d+)|(\d+(\.\d*)?))([eE][+-]?\d+)?$')
        return bool(regex.match(s.strip()))

其实还可以简化一些

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

思路 4 - 时间复杂度: O(N)- 空间复杂度: O(1)******

但是我还是最喜欢下面这种方法,皮一下很开心!!

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