forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_stack_n.cpp
45 lines (39 loc) · 1014 Bytes
/
AC_stack_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_stack_n.cpp
* Create Date: 2015-03-15 15:53:45
* Descripton: Using stack && atoi
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> stk;
string op = "+-*/";
for (auto str : tokens) {
auto pos = op.find(str);
if (pos == string::npos) {
stk.push(atoi(str.c_str()));
} else {
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
if (pos == 0)
stk.push(b + a);
else if (pos == 1)
stk.push(b - a);
else if (pos == 2)
stk.push(b * a);
else if (pos == 3)
stk.push(b / a);
}
}
return stk.top();
}
};
int main() {
return 0;
}