Skip to content

Commit 7e29011

Browse files
authored
Improved Node and task 150
1 parent 5ab98b0 commit 7e29011

File tree

2 files changed

+10
-19
lines changed
  • src/main/kotlin

2 files changed

+10
-19
lines changed

src/main/kotlin/com_github_leetcode/Node.kt

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com_github_leetcode
22

3-
import java.util.StringJoiner
4-
import kotlin.collections.ArrayList
5-
63
class Node {
74
var `val`: Int
85
var neighbors: List<Node>
@@ -23,18 +20,14 @@ class Node {
2320
}
2421

2522
override fun toString(): String {
26-
val result = StringJoiner(",", "[", "]")
27-
for (node in neighbors) {
23+
return neighbors.joinToString(separator = ",", prefix = "[", postfix = "]") { node ->
2824
if (node.neighbors.isEmpty()) {
29-
result.add(node.`val`.toString())
25+
node.`val`.toString()
3026
} else {
31-
val result2 = StringJoiner(",", "[", "]")
32-
for (nodeItem in node.neighbors) {
33-
result2.add(nodeItem.`val`.toString())
27+
node.neighbors.joinToString(separator = ",", prefix = "[", postfix = "]") { nodeItem ->
28+
nodeItem.`val`.toString()
3429
}
35-
result.add(result2.toString())
3630
}
3731
}
38-
return result.toString()
3932
}
4033
}

src/main/kotlin/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution.kt

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ package g0101_0200.s0150_evaluate_reverse_polish_notation
33
// #Medium #Top_Interview_Questions #Array #Math #Stack #Programming_Skills_II_Day_3
44
// #2022_10_09_Time_233_ms_(88.82%)_Space_36.7_MB_(91.45%)
55

6-
import java.util.function.BiFunction
7-
86
class Solution {
9-
val op = mapOf<String, BiFunction<Int, Int, Int>>(
10-
"/" to BiFunction { a, b -> a / b },
11-
"*" to BiFunction { a, b -> a * b },
12-
"+" to BiFunction { a, b -> a + b },
13-
"-" to BiFunction { a, b -> a - b }
7+
val op = mapOf<String, (Int, Int) -> Int>(
8+
"/" to { a, b -> a / b },
9+
"*" to { a, b -> a * b },
10+
"+" to { a, b -> a + b },
11+
"-" to { a, b -> a - b }
1412
)
1513
fun evalRPN(tokens: Array<String>): Int {
1614
val stack = ArrayDeque<String>()
1715
for (t in tokens) {
1816
if (op.contains(t)) {
1917
val b = stack.removeFirst().toInt()
2018
val a = stack.removeFirst().toInt()
21-
val c = op.getValue(t).apply(a, b)
19+
val c = op.getValue(t).invoke(a, b)
2220
stack.addFirst(c.toString())
2321
} else {
2422
stack.addFirst(t)

0 commit comments

Comments
 (0)