-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path424.h
39 lines (39 loc) · 1.09 KB
/
424.h
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
class Solution {
public:
/*
* @param tokens: The Reverse Polish Notation
* @return: the value
*/
int evalRPN(vector<string> &tokens) {
// write your code here
stack<int> s;
for(auto ite = tokens.begin(); ite != tokens.end(); ite++){
int num1, num2;
if(*ite == "+"){
num2 = s.top(); s.pop();
num1 = s.top(); s.pop();
s.push(num1 + num2);
}
else if(*ite == "-"){
num2 = s.top(); s.pop();
num1 = s.top(); s.pop();
s.push(num1 - num2);
}
else if(*ite == "*"){
num2 = s.top(); s.pop();
num1 = s.top(); s.pop();
s.push(num1 * num2);
}
else if(*ite == "/"){
num2 = s.top(); s.pop();
num1 = s.top(); s.pop();
s.push(num1 / num2);
}
else{
s.push(stoi(*ite));
}
cout << s.top() << endl;
}
return s.top();
}
};