lurenaa的博客

🥩stack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> stk;
for(auto c : tokens)
{
int nu = atoi(c.c_str());
if(!nu && c != "0") {
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
if(c == "+") {
stk.push(a + b);
}
else if (c == "-") {
stk.push(b - a);
}
else if (c == "/"){
stk.push(b / a);
}
else {
stk.push(b * a);
}
}
else
stk.push(nu);
}
return stk.top();
}
};

Accepted

20/20 cases passed (24 ms)

Your runtime beats 25.49 % of cpp submissions

Your memory usage beats 9.09 % of cpp submissions (11.8 MB)