diff --git a/src/main/kotlin/com_github_leetcode/Node.kt b/src/main/kotlin/com_github_leetcode/Node.kt index 4d435c9b3..f65b9473f 100644 --- a/src/main/kotlin/com_github_leetcode/Node.kt +++ b/src/main/kotlin/com_github_leetcode/Node.kt @@ -1,8 +1,5 @@ package com_github_leetcode -import java.util.StringJoiner -import kotlin.collections.ArrayList - class Node { var `val`: Int var neighbors: List @@ -23,18 +20,14 @@ class Node { } override fun toString(): String { - val result = StringJoiner(",", "[", "]") - for (node in neighbors) { + return neighbors.joinToString(separator = ",", prefix = "[", postfix = "]") { node -> if (node.neighbors.isEmpty()) { - result.add(node.`val`.toString()) + node.`val`.toString() } else { - val result2 = StringJoiner(",", "[", "]") - for (nodeItem in node.neighbors) { - result2.add(nodeItem.`val`.toString()) + node.neighbors.joinToString(separator = ",", prefix = "[", postfix = "]") { nodeItem -> + nodeItem.`val`.toString() } - result.add(result2.toString()) } } - return result.toString() } } diff --git a/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution.kt b/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution.kt index 36a637edc..ced8f2045 100644 --- a/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution.kt +++ b/src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution.kt @@ -3,14 +3,12 @@ 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 } + val op = mapOf Int>( + "/" to { a, b -> a / b }, + "*" to { a, b -> a * b }, + "+" to { a, b -> a + b }, + "-" to { a, b -> a - b } ) fun evalRPN(tokens: Array): Int { val stack = ArrayDeque() @@ -18,7 +16,7 @@ class Solution { if (op.contains(t)) { val b = stack.removeFirst().toInt() val a = stack.removeFirst().toInt() - val c = op.getValue(t).apply(a, b) + val c = op.getValue(t).invoke(a, b) stack.addFirst(c.toString()) } else { stack.addFirst(t)