-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem150.cpp
28 lines (26 loc) · 879 Bytes
/
problem150.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
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int>s;
for(auto token : tokens){
if(token.size() > 1 || isdigit(token[0]))
s.push(stoi(token));
else{
int x = s.top();
s.pop();
int y = s.top();
s.pop();
int result = 0;
if(token == "+")
s.push(x+y);
else if(token == "-")
s.push(y-x);
else if(token == "*")
s.push(x*y);
else
s.push(y/x);
}
}
return s.top();
}
};