Skip to content

Commit bad7156

Browse files
committed
Solved problem 150 : Evaluate Reverse Polish Notation
1 parent 28f1283 commit bad7156

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### Problems Solved
1818

19-
| Total | 59 |
19+
| Total | 60 |
2020
|:---:|:---:|
2121

2222
#### Search By Topic
@@ -38,7 +38,7 @@
3838
| Math & Geometry | 4 |
3939
| Priority Queue | 3 |
4040
| Sliding Window | 2 |
41-
| Stack | 2 |
41+
| Stack | 3 |
4242
| Tries | 0 |
4343
| Two Pointers | 6 |
4444

@@ -47,7 +47,7 @@
4747
| Difficulty | Number |
4848
|:---|---:|
4949
| Easy | 48 |
50-
| Medium | 11 |
50+
| Medium | 12 |
5151
| Hard | 0 |
5252

5353
## Milestones
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Problem: 150
3+
* Name: Evaluate Reverse Polish Notation
4+
* Difficulty: Medium
5+
* Topic: Stack
6+
* Link: https://leetcode.com/problems/evaluate-reverse-polish-notation/
7+
*/
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
// Stack Structure
13+
// Time Complexity: O(n)
14+
// Space Complexity: O(n)
15+
int evalRPN(vector<string>& tokens) {
16+
stack<int> values;
17+
18+
for (const string& s : tokens) {
19+
if (s == "+"){
20+
int a = values.top(); values.pop();
21+
int b = values.top(); values.pop();
22+
values.push(b + a);
23+
}
24+
else if (s == "-"){
25+
int a = values.top(); values.pop();
26+
int b = values.top(); values.pop();
27+
values.push(b - a);
28+
}
29+
else if (s == "*"){
30+
int a = values.top(); values.pop();
31+
int b = values.top(); values.pop();
32+
values.push(b * a);
33+
}
34+
else if (s == "/"){
35+
int a = values.top(); values.pop();
36+
int b = values.top(); values.pop();
37+
values.push(b / a);
38+
}
39+
else {
40+
values.push(stoi(s));
41+
}
42+
}
43+
return values.top();
44+
}

0 commit comments

Comments
 (0)