-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150_new.cpp
39 lines (38 loc) · 1.07 KB
/
150_new.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
#include<vector>
#include<iostream>
#include<stack>
#include<string>
//#include<unordered_set>
#include<climits>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
//int res;
for (auto s : tokens) {
if (s != "+" && s != "-" && s != "*" && s != "/") { //是数字
st.push(stoi(s));
} else {
int num2 = st.top();
st.pop();
int num1 = st.top();
st.pop();
if (s == "+") {
st.push(num1 + num2);
} else if (s == "-") {
st.push(num1 - num2);
} else if (s == "*") {
st.push(num1 * num2);
} else if (s == "/") {
st.push((int)(num1 / num2));
}
}
}
return st.top();
}
};
int main() {
vector<string> tokens = {"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
cout << (new Solution)->evalRPN(tokens) << endl;
}