-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path150.evaluate-reverse-polish-notation.cpp
More file actions
46 lines (43 loc) · 1.04 KB
/
150.evaluate-reverse-polish-notation.cpp
File metadata and controls
46 lines (43 loc) · 1.04 KB
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
/*
* @lc app=leetcode id=150 lang=cpp
*
* [150] Evaluate Reverse Polish Notation
*/
// @lc code=start
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long long> stk;
for(auto a:tokens){
if(isOper(a)){
long first=stk.top();
stk.pop();
long second=stk.top();
stk.pop();
if(a=="+"){
stk.push(first+second);
}
else if(a=="-"){
stk.push(second-first);
}
else if (a=="*"){
stk.push((long long)first*second);
}
else if(a=="/"){
stk.push((second/first));
}
}
else{
stk.push(stoi(a));
}
}
return stk.top();
}
bool isOper(string c){
if(c=="+" || c=="-"|| c=="/"|| c=="*"){
return true;
}
return false;
}
};
// @lc code=end