Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b9c25c1

Browse files
committed
Time: 25 ms (29.08%), Space: 39 MB (43.14%) - LeetHub
1 parent 7224aee commit b9c25c1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)