-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvaluate Reverse Polish Notation.cpp
68 lines (61 loc) · 1.19 KB
/
Evaluate Reverse Polish Notation.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
61
62
63
64
65
66
67
68
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <stack>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int>cache;
for (int i = 0; i < tokens.size(); i++)
{
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/")
{
int num1 = cache.top();
cache.pop();
int num2 = cache.top();
cache.pop();
cache.push(Calculate(num2, num1, tokens[i][0]));
}
else
{
cache.push(str2int(tokens[i]));
}
}
return cache.top();
}
private:
int Calculate(int num1, int num2, char op)
{
int cache;
switch (op)
{
case '*':cache = num1*num2; break;
case '+':cache = num1 + num2; break;
case '-':cache = num1 - num2; break;
case '/':cache = num1 / num2; break;
default:
break;
}
return cache;
}
int str2int(string str)
{
int result = 0;
int sign = 1;
if (str[0] == '-')
sign = -1;
else
result = ((int)str[0] - 48) + result * 10;
for (int i = 1; i < str.size(); i++)
{
if (str[0])
result = ((int)str[i] - 48) + result * 10;
}
return result*sign;
}
};