We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7224aee commit b9c25c1Copy full SHA for b9c25c1
0150-evaluate-reverse-polish-notation/0150-evaluate-reverse-polish-notation.kt
@@ -0,0 +1,31 @@
1
+import java.util.Stack
2
+
3
+class Solution {
4
+ private val OPERATORS = arrayOf(
5
+ "+", "-", "*", "/"
6
+ )
7
8
+ fun evalRPN(tokens: Array<String>): Int {
9
+ val numbers = Stack<Int>()
10
11
+ for (token in tokens) {
12
+ if (token !in OPERATORS) {
13
+ numbers.push(token.toInt())
14
+ } else {
15
+ val op2 = numbers.pop()
16
+ val op1 = numbers.pop()
17
18
+ val result = when (token) {
19
+ "+" -> op1 + op2
20
+ "-" -> op1 - op2
21
+ "*" -> op1 * op2
22
+ else -> op1 / op2
23
+ }
24
25
+ numbers.push(result)
26
27
28
29
+ return numbers.pop()
30
31
+}
0 commit comments