「Leetcode」字符串相关问题选编
剑指 Offer 20. 表示数值的字符串
这题介绍一个使用有限自动机的做法,实际上细心的考虑corner case的效率比这个方法更好,但是这个方法比较具有代表性。
画出如下的示意图:
然后照着这个示意图做就可以了。
注意,这么几种样例都是合法的:
-1.e-12
+.1
+1.
-.52E+3
114.514e123
接下来见代码:
from enum import Enum
class Solution:
def isNumber(self, s: str) -> bool:
State = Enum('State', (
'START', 'SYM_START_DECIMAL_PNT', 'SYM_MID_DECIMAL_PNT', 'NUM_SIMPLE',
'NUM_FIRST_PART', 'NUM_SECOND_PART', 'SYM_SCI', 'NUM_AFT_SCI',
'SYM_PN_SIMPLE', 'SYM_PN_SCI', 'ERROR'))
end_state = (
State.NUM_SIMPLE, State.SYM_MID_DECIMAL_PNT, State.NUM_FIRST_PART,
State.NUM_SECOND_PART, State.NUM_AFT_SCI)
now_state = State.START
s = s.strip()
s_iter = iter(s)
while True:
if now_state == State.ERROR:
return False
try:
now_chr = next(s_iter)
except StopIteration:
if now_state in end_state:
return True
else:
return False
if now_state == State.START:
if now_chr.isnumeric():
now_state = State.NUM_FIRST_PART
continue
elif now_chr in ['+', '-']:
now_state = State.SYM_PN_SIMPLE
continue
elif now_chr == ".":
now_state = State.SYM_START_DECIMAL_PNT
continue
elif now_state == State.NUM_FIRST_PART:
if now_chr.isnumeric():
continue
elif now_chr == ".":
now_state = State.SYM_MID_DECIMAL_PNT
continue
elif now_chr in ["e", "E"]:
now_state = State.SYM_SCI
continue
elif now_state == State.SYM_START_DECIMAL_PNT:
if now_chr.isnumeric():
now_state = State.NUM_SIMPLE
continue
elif now_state == State.SYM_MID_DECIMAL_PNT:
if now_chr.isnumeric():
now_state = State.NUM_SECOND_PART
continue
elif now_chr in ["e", "E"]:
now_state = State.SYM_SCI
continue
elif now_state == State.NUM_SIMPLE:
if now_chr in ["e", "E"]:
now_state = State.SYM_SCI
continue
elif now_chr.isnumeric():
continue
elif now_state == State.NUM_SECOND_PART:
if now_chr.isnumeric():
continue
elif now_chr in ["e", "E"]:
now_state = State.SYM_SCI
continue
elif now_state == State.SYM_SCI:
if now_chr.isnumeric():
now_state = State.NUM_AFT_SCI
continue
elif now_chr in ["+", "-"]:
now_state = State.SYM_PN_SCI
continue
elif now_state == State.NUM_AFT_SCI:
if now_chr.isnumeric():
continue
elif now_state == State.SYM_PN_SIMPLE:
if now_chr == ".":
now_state = State.SYM_START_DECIMAL_PNT
continue
elif now_chr.isnumeric():
now_state = State.NUM_FIRST_PART
continue
elif now_state == State.SYM_PN_SCI:
if now_chr.isnumeric():
now_state = State.NUM_AFT_SCI
continue
now_state = State.ERROR
如非注明,原创内容遵循GFDLv1.3发布;其中的代码遵循GPLv3发布。