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