-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversepolishnotation.cpp
More file actions
34 lines (34 loc) · 945 Bytes
/
reversepolishnotation.cpp
File metadata and controls
34 lines (34 loc) · 945 Bytes
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
class Solution
{
public:
int evalRPN(vector<string> &tokens)
{
stack<int> s1;
int temp1, temp2, temp;
int len = tokens.size();
for (int i = 0; i < len; i++)
{
if (tokens[i] == "*" || tokens[i] == "+" || tokens[i] == "/" || tokens[i] == "-")
{
temp2 = s1.top();
s1.pop();
temp1 = s1.top();
s1.pop();
if (tokens[i] == "+")
temp = temp1 + temp2;
else if (tokens[i] == "-")
temp = temp1 - temp2;
else if (tokens[i] == "*")
temp = temp1 * temp2;
else if (tokens[i] == "/")
temp = temp1 / temp2;
s1.push(temp);
}
else
{
s1.push(stoi(tokens[i]));
}
}
return s1.top();
}
};