Valid Number
今天做了这题https://oj.leetcode.com/problems/valid-number/
感觉是作弊么。。。。python 还是屌
1
2
3
4
5
try:
float(s)
return True
except:
return False
OK 试试不使用try玩
唉,试了字符判断,各种不够优雅
除去前后空白,将字符处理成简单符号,要判断的情况有以下几种:
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
37
38
39
40
41
- n
- sn
- dn
- nd
- nen
- ndn
- snd
- nen
- sdn
- nden
- sndn
- nesn
- snen
- dnen
- sdnen
- ndnen
- ndesn
- snden
- dnesn
- snesn
- sdnesn
- sndnen
- ndnesn
s=s.strip()
S=' '
for i in s:
if i=='-' or i=='+':
S=S+'s'
elif i=='.':
S=S+'d'
elif i=='e' or i=='E':
S=S+'e'
elif i>='0' and i<='9':
if not S[-1]=='n':
S=S+'n'
else:
return False
S=S.strip()
冗余代码略
return False
This post is licensed under CC BY 4.0 by the author.