-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150.cpp
50 lines (50 loc) · 1.31 KB
/
150.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
45
46
47
48
49
50
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(string s : tokens) {
if(s == "+" || s == "-" || s == "*" || s == "/")
{
int s_op = st.top();
st.pop();
int f_op = st.top();
st.pop();
int tem;
switch(s[0]) {
case '+':
tem = f_op + s_op;
break;
case '-':
tem = f_op - s_op;
break;
case '*':
tem = f_op * s_op;
break;
case '/':
tem = f_op / s_op;
break;
}
st.push(tem);
} else {
int s_int = str2int(s);
st.push(s_int);
}
}
int res = st.top();
return res;
}
private:
int str2int(string s) {
int res = 0, len = s.size();
bool neg_tag = false;
int i = 0;
if(s[0] == '-')
{
neg_tag = true;
i++;
}
for(; i < len; i++)
res = res * 10 + s[i] - '0';
return neg_tag ? -res : res;
}
};