import re
import collections
class Solution(object):
def countOfAtoms(self, formula):
parse = re.findall(r"([A-Z][a-z])(d)|(()|())(d*)", formula)
stack = [collections.Counter()]
for name, m1, left_open, right_open, m2 in parse:
if name:
stack[-1][name] += int(m1 or 1)
if left_open:
stack.append(collections.Counter())
if right_open:
top = stack.pop()
for k in top:
stack[-1][k] += top[k] * int(m2 or 1)
return “”.join(name + (str(stack[-1][name]) if stack[-1][name] > 1 else ‘’) for name in sorted(stack[-1]))
#主函数
if name == ‘main’:
solution = Solution()
Test_in = “H2O”
Test_out = solution.countOfAtoms(Test_in)
print(“输入为:”,Test_in)
print(“输出为:”,Test_out)
原子的数量个
最新推荐文章于 2024-07-15 12:10:21 发布