forked from Nandinig24/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150
52 lines (41 loc) · 1.18 KB
/
150
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
class Solution {
public:
int evalRPN(vector<string>& to) {
stack<string>s;
for(int i=0;i<to.size();i++){
if(to[i]=="+" || to[i]=="-"|| to[i]=="*" || to[i]=="/"){
string a=s.top();
s.pop();
string b=s.top();
s.pop();
int a1=stoi(a);
int b1=stoi(b);
if(to[i]=="-"){
int r=b1-a1;
s.push(to_string(r));
}
if(to[i]=="+"){
int r=a1+b1;
s.push(to_string(r));
}
if(to[i]=="*"){
int r=a1*b1;
s.push(to_string(r));
}
if(to[i]=="/"){
if(a1!=0){
int r=b1/a1;
s.push(to_string(r));
}
else
return 0;
}
}
else{
s.push(to[i]);
}
}
string p=s.top();
return stoi(p);
}
};