-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path150. Evaluate Reverse Polish Notation.cpp
More file actions
87 lines (80 loc) · 2.15 KB
/
150. Evaluate Reverse Polish Notation.cpp
File metadata and controls
87 lines (80 loc) · 2.15 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
int top=0,j,temp;
bool sign;
//cout<<tokens.size();
vector<int> result;
for(int i=0;i<tokens.size();i++){
//cout<<tokens[i].length()<<endl;
switch(tokens[i][0]){
case '+':
result[top-2]+=result[top-1];
result.pop_back();
top--;
break;
case '-':
result[top-2]-=result[top-1];
result.pop_back();
top--;
break;
case '*':
result[top-2]*=result[top-1];
result.pop_back();
top--;
break;
case '/':
result[top-2]/=result[top-1];
result.pop_back();
top--;
break;
default:
temp=0;
sign=false;
for(j=0;j<tokens[i].length();j++){
if(tokens[i][j]=='-'){
sign=true;
continue;
}
temp*=10;
temp+=(tokens[i][j]-48);
//cout<<temp<<endl;
}
if(sign){
temp=-temp;
}
result.push_back(temp);
top++;
}
/*for(int k=0;k<result.size();k++){
cout<<result[k]<<' ';
}
cout<<endl;*/
}
return result[0];
}
};
int main(void){
Solution my;
vector<string> input;
//["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
input.push_back("10");
input.push_back("6");
input.push_back("9");
input.push_back("3");
input.push_back("+");
input.push_back("-11");
input.push_back("*");
input.push_back("/");
input.push_back("*");
input.push_back("17");
input.push_back("+");
input.push_back("5");
input.push_back("+");
cout<<my.evalRPN(input);
return 0;
}