![JAR search and dependency download from the Maven repository](/logo.png)
g0101_0200.s0150_evaluate_reverse_polish_notation.Solution.kt Maven / Gradle / Ivy
package g0101_0200.s0150_evaluate_reverse_polish_notation
// #Medium #Top_Interview_Questions #Array #Math #Stack #Programming_Skills_II_Day_3
// #2022_10_09_Time_233_ms_(88.82%)_Space_36.7_MB_(91.45%)
import java.util.function.BiFunction
class Solution {
val op = mapOf>(
"/" to BiFunction { a, b -> a / b },
"*" to BiFunction { a, b -> a * b },
"+" to BiFunction { a, b -> a + b },
"-" to BiFunction { a, b -> a - b }
)
fun evalRPN(tokens: Array): Int {
val stack = ArrayDeque()
for (t in tokens) {
if (op.contains(t)) {
val b = stack.removeFirst().toInt()
val a = stack.removeFirst().toInt()
val c = op[t]!!.apply(a, b)
stack.addFirst(c.toString())
} else {
stack.addFirst(t)
}
}
return stack.removeFirst().toInt()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy