卖萌的弱渣

I am stupid, I am hungry.

Valid Number

Validate if a given string is numeric.

Some examples:

1
2
3
4
5
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note:

It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

  • 小数点前和后没有数字都是合法的
  • 科学计数法后面也可以是负数

    Solution

(Valid-Number.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution(object):
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        ####
        # transitionTable[state][inputtype] = value
        ####

        # state: 0==invalid, 1==space, 2==sign, 3==digit, 4==dot, 5==exponent, 6==num_inputs
        # inputtype: 如下
        INVALID=0; SPACE=1; SIGN=2; DIGIT=3; DOT=4; EXPONENT=5
        # value:
        # 0 no input or just spaces
        # 1 input is digits
        # 2 no digits in front just Dot
        # 3 sign
        # 4 digits and dot in front
        # 5 input e or E (exponent)
        # 6 after e input sign
        # 7 after e input digits
        # after valid input input space
        state=0; i=0
        while i<len(s):
            inputtype = INVALID
            if s[i]==' ': inputtype=SPACE
            elif s[i]=='-' or s[i]=='+': inputtype=SIGN
            elif s[i] in '0123456789': inputtype=DIGIT
            elif s[i]=='.': inputtype=DOT
            elif s[i]=='e' or s[i]=='E': inputtype=EXPONENT

            state=transitionTable[state][inputtype]
            if state==-1: return False
            else: i+=1
        return state == 1 or state == 4 or state == 7 or state == 8