-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
31 lines (30 loc) · 897 Bytes
/
solution.cpp
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
/**
* 20 / 20 test cases passed.
* Runtime: 12 ms
* Memory Usage: 11.5 MB
*/
class Solution {
public:
int evalRPN(vector<string>& tokens) {
vector<int> nums(tokens.size());
int top = -1;
for (auto& token: tokens) {
if ("+" == token) {
int result = nums[top - 1] + nums[top];
nums[--top] = result;
} else if ("-" == token) {
int result = nums[top - 1] - nums[top];
nums[--top] = result;
} else if ("*" == token) {
int result = nums[top - 1] * nums[top];
nums[--top] = result;
} else if ("/" == token) {
int result = nums[top - 1] / nums[top];
nums[--top] = result;
} else {
nums[++top] = stoi(token);
}
}
return nums[top];
}
};