-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path220426_65_ValidNumber_Hard.py
50 lines (45 loc) · 1.68 KB
/
220426_65_ValidNumber_Hard.py
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
42
43
44
45
46
47
48
49
50
# String
import re
class Solution:
# 참고
# https://leetcode.com/problems/valid-number/discuss/173977/Python-with-simple-explanation
def isNumber(self, s: str) -> bool:
has_e = has_dot = has_num = False
for i, ch in enumerate(s):
if ch in ['+', '-']:
if i > 0 and s[i - 1] not in ['e', 'E']: # +, - 앞은 e/E 이어야 함
return False
elif i == len(s) - 1:
return False
elif ch == '.':
if has_dot or has_e: # '.'이 2번 이상 또는 e 다음에 소수가 오는 경우
return False
elif not has_num and i == len(s) - 1:
return False
has_dot = True
elif ch in ['e', 'E']:
if not has_num or has_e or i == len(s) - 1:
return False
has_e = True
elif not ch.isdigit():
return False
else:
has_num = True
return True
# 참고
# https://leetcode.com/problems/valid-number/discuss/23739/Easy-Python-Solution-68-ms-beats-100
def isNumber2(self, s: str) -> bool:
if s in ["inf", "-inf", "+inf", "Infinity", "-Infinity", "+Infinity"]:
return False
try:
float(s)
except ValueError:
return False
else:
return True
# 참고
# https://leetcode.com/problems/valid-number/discuss/348874/Python-3-Regex-with-example
def isNumber3(self, s: str) -> bool:
# ?: 있거나 없거나
p = re.compile('^[+-]?((\d+\.?\d*)|(\d*\.?\d+))([eE][+-]?\d+)?$')
return p.match(s)