Skip to content

Commit 397d338

Browse files
authored
Merge pull request #2260 from ishworpanta10/patch-3
Create 0150-evaluate-reverse-polish-notation.dart
2 parents 4e78470 + c8f003a commit 397d338

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
int evalRPN(List<String> tokens) {
3+
var stack = [];
4+
for (var i = 0; i < tokens.length; i++) {
5+
var char = tokens[i];
6+
if (char == '+') {
7+
var a = stack.removeLast();
8+
var b = stack.removeLast();
9+
stack.add(a + b);
10+
} else if (char == '-') {
11+
var a = stack.removeLast();
12+
var b = stack.removeLast();
13+
stack.add(b - a);
14+
} else if (char == '*') {
15+
var a = stack.removeLast();
16+
var b = stack.removeLast();
17+
stack.add(a * b);
18+
} else if (char == '/') {
19+
var a = stack.removeLast();
20+
var b = stack.removeLast();
21+
stack.add(b ~/ a);
22+
} else {
23+
stack.add(int.parse(char));
24+
}
25+
}
26+
return stack.first;
27+
}
28+
}

0 commit comments

Comments
 (0)