-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvaluateReversePolishNotation_150.cpp
60 lines (58 loc) · 1.66 KB
/
EvaluateReversePolishNotation_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
51
52
53
54
55
56
57
58
59
60
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 150. Evaluate Reverse Polish Notation
~ Link : https://leetcode.com/problems/evaluate-reverse-polish-notation/
*/
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> st;
int s1,s2;
s1=s2=0;
int res=0;
for(vector<string>::iterator iter=tokens.begin();iter!=tokens.end();iter++)
{
if (*iter == "+")
{
s1=st.top();
st.pop();
s2=st.top();
st.pop();
res=s1+s2;
st.push(res);
}
else if (*iter == "-")
{
s1=st.top();
st.pop();
s2=st.top();
st.pop();
res=s2-s1;
st.push(res);
}
else if (*iter == "*")
{
s1=st.top();
st.pop();
s2=st.top();
st.pop();
res=s1*s2;
st.push(res);
}
else if (*iter== "/")
{
s1=st.top();
st.pop();
s2=st.top();
st.pop();
res=s2/s1;
st.push(res);
}
else
{
st.push(atoi((*iter).c_str()));
}
}
return st.top();
}
};