-
Notifications
You must be signed in to change notification settings - Fork 0
/
150.evaluate-reverse-polish-notation.cpp
55 lines (49 loc) · 1.19 KB
/
150.evaluate-reverse-polish-notation.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @lc app=leetcode id=150 lang=cpp
*
* [150] Evaluate Reverse Polish Notation
*/
// @lc code=start
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> s;
for (string& str: tokens){
if (str[0] == '+' && str.length() == 1){
int a = s.top();
s.pop();
a += s.top();
s.pop();
s.push(a);
continue;
}
if (str[0] == '-' && str.length() == 1){
int a = -s.top();
s.pop();
a += s.top();
s.pop();
s.push(a);
continue;
}
if (str[0] == '*'){
int a = s.top();
s.pop();
a *= s.top();
s.pop();
s.push(a);
continue;
}
if (str[0] == '/'){
int a = s.top();
s.pop();
a = s.top() / a;
s.pop();
s.push(a);
continue;
}
s.push(stoi(str));
}
return s.top();
}
};
// @lc code=end