Re: [闲聊] 每日leetcode

楼主: smart0eddie (smart0eddie)   2024-07-14 10:33:02
20240714
726. Number of Atoms
Given a string formula representing a chemical formula, return the count of
each atom.
The atomic element always starts with an uppercase character, then zero or
more lowercase letters, representing the name.
One or more digits representing that element's count may follow if the count
is greater than 1. If the count is 1, no digits will follow.
For example, "H2O" and "H2O2" are possible, but "H1O2" is impossible.
Two formulas are concatenated together to produce another formula.
For example, "H2O2He3Mg4" is also a formula.
A formula placed in parentheses, and a count (optionally added) is also a
formula.
For example, "(H2O2)" and "(H2O2)3" are formulas.
Return the count of all elements as a string in the following form: the first
name (in sorted order), followed by its count (if that count is more than 1),
followed by the second name (in sorted order), followed by its count (if that
count is more than 1), and so on.
The test cases are generated so that all the values in the output fit in a
32-bit integer.
有事情没空写继续抄解答.jpg
基本上会分两块
一块是先拆解input 同时计数
一块就是输出
输出就把元素照顺序排好以后照格式输出
没什么特别的
输入parsing 的部分
应该会是用<str, int> dict 去记目前的元素个数
逐字走过去
碰到不是小写的就是元素分隔符
把前面的元素拆出来加上数量
加进dict
另外要考虑到(A2B3)4这种鸟东西
可能要另外一个stack 来存目前的元素
或是先扫一次把括号抓出来

Links booklink

Contact Us: admin [ a t ] ucptt.com