逐个字符读取,总共分为三类字符:括号、数值、原子(字母)
使用数据结构:双端队列(deque,用作栈)、字典(defaultdict)
class Solution(object):
def countOfAtoms(self, formula):
"""
:type formula: str
:rtype: str
"""
# 双端队列,作为栈
d = deque()
# 字典,新建值默认为1
map1 = defaultdict(lambda:1)
n = len(formula)
i = atom_index = 0
while i < n:
c = formula[i]
# 括号直接入栈
if c=='(' or c==')':
d.append(c)
i += 1
# 获取完整数字
elif c.isdigit():
j = i
while j
代码性能



