Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2024-07-14 14:26:55
※ 引述 《smart0eddie (smart0eddie)》 之銘言:
:  
: 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), ollowed 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.
:  
翻譯:
問你這些化學的甚麼小雞巴式子裡面
有什麼元素 多少個
思路:
有()然後要配對的stack
可是裡面的東西是元素
有可能是Mg這種賤字串
所以要往右邊找
所以很麻煩
然後字母跟數字跟刮號都混在一起
所以要isdigit isupper islower
所以很麻煩
還要看括號右邊有沒有數字
檢查的時候還要注意有沒有越界
所以很麻煩
然後數字還有可能不只一位數字
所以要一直往右邊找 同時不能越界
所以很麻煩
幹你娘機掰
我這題寫到快爆氣
我丟給gpt才改好的
我快吐了
這題要分成兩個部分
一個是用stack檢查在哪部分的元素的倍率
然後紀錄下來
另一個是用map收集元素
同時紀錄當前的倍率
並且在map的那個元素加上當前的倍率
然後再把東西丟回去string
我快哭了
有沒有人要打lol
```cpp
class Solution {
public:
string countOfAtoms(string formula)
{
int len = formula.size();
int p = 0;
unordered_map<string,int> elem;
vector<int> save;
vector<int> paper(len,1);
for(int i = 0 ; i < len ; i ++)
{
if(formula[i]=='(' ){
save.push_back(i);
continue;
}
if(formula[i]==')' ){
int dig = 0;
int j = i + 1;
while (j < len && isdigit(formula[j]) )
{
dig = dig * 10 + formula[j] - '0';
j++;
}
dig = max(dig, 1);
paper[save.back()] = dig;
save.pop_back();
paper[i] = -dig;
i = j - 1;
continue;
}
}
int mult = 1;
for(int i = 0 ; i < len ; i ++)
{
if(paper[i]>0){
mult*=paper[i];
}
else{
mult/=abs(paper[i]);
}
if (isupper(formula[i]))
{
string k(1, formula[i]);
int j = i + 1;
while (j < len && islower(formula[j]))
{
k += formula[j];
j++;
}
int count = 0;
while (j < len && isdigit(formula[j]))
{
count = count*10 + formula[j]-'0';
j++;
}
count = max(count, 1);
elem[k] += count * mult;
i = j - 1;
}
}
string res ;
map<string, int> elem2(elem.begin(), elem.end());
for(auto k : elem2)
{
res+=k.first;
if(k.second>1){
res+=to_string(k.second);
}
}
return res;
}
};
```
作者: mrsonic (typeB)   2024-07-14 14:27:00
你有甚麼用
作者: Furina (芙寧娜)   2024-07-14 14:27:00
我好崇拜你
作者: sustainer123 (caster)   2024-07-14 14:29:00
大師
作者: CanIndulgeMe (CIM)   2024-07-14 14:33:00
技術大神
作者: JIWP (JIWP)   2024-07-14 14:33:00
我好崇拜你
作者: smart0eddie (smart0eddie)   2024-07-14 15:10:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com