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