Skip to content

Commit 8438130

Browse files
authored
Improved tasks 112, 115, 116, 117, 120, 126
1 parent 6aa50a9 commit 8438130

File tree

6 files changed

+18
-28
lines changed

6 files changed

+18
-28
lines changed

src.save/main/kotlin/g0101_0200/s0112_path_sum/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import com_github_leetcode.TreeNode
1616
* }
1717
*/
1818
class Solution {
19-
fun hasPathSum(root: TreeNode?, sum: Int): Boolean {
19+
fun hasPathSum(root: TreeNode?, targetSum: Int): Boolean {
2020
if (root == null) {
2121
return false
2222
}
23-
return if (sum == root.`val` && root.left == null && root.right == null) {
23+
return if (targetSum == root.`val` && root.left == null && root.right == null) {
2424
true
25-
} else hasPathSum(root.left, sum - root.`val`) || hasPathSum(root.right, sum - root.`val`)
25+
} else hasPathSum(root.left, targetSum - root.`val`) || hasPathSum(root.right, targetSum - root.`val`)
2626
}
2727
}

src.save/main/kotlin/g0101_0200/s0115_distinct_subsequences/Solution.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ package g0101_0200.s0115_distinct_subsequences
33
// #Hard #String #Dynamic_Programming #2022_09_29_Time_285_ms_(88.89%)_Space_34.2_MB_(100.00%)
44

55
class Solution {
6-
fun numDistinct(text: String, text2: String): Int {
7-
if (text.length < text2.length) {
6+
fun numDistinct(s: String, t: String): Int {
7+
if (s.length < t.length) {
88
return 0
99
}
10-
if (text.length == text2.length) {
11-
return if (text == text2) 1 else 0
10+
if (s.length == t.length) {
11+
return if (s == t) 1 else 0
1212
}
13-
val move = text.length - text2.length + 2
13+
val move = s.length - t.length + 2
1414
// Only finite number of character in s can occupy first position in T. Same applies for
1515
// every character in T.
1616
val dp = IntArray(move)
1717
var j = 1
1818
var k = 1
19-
for (i in 0 until text2.length) {
19+
for (i in 0 until t.length) {
2020
var firstMatch = true
2121
while (j < move) {
22-
if (text2[i] == text[i + j - 1]) {
22+
if (t[i] == s[i + j - 1]) {
2323
if (firstMatch) {
2424
// Keep track of first match. To avoid useless comparisons on next
2525
// iteration.

src.save/main/kotlin/g0101_0200/s0116_populating_next_right_pointers_in_each_node/Solution.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import com_github_leetcode.left_right.Node
1414
* var next: Node? = null
1515
* }
1616
*/
17-
1817
class Solution {
1918
fun connect(root: Node?): Node? {
2019
if (root == null) {

src.save/main/kotlin/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii/Solution.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,23 @@ import java.util.Queue
1818
*/
1919
class Solution {
2020
fun connect(root: Node?): Node? {
21-
2221
if (root == null) return null
23-
2422
val bfsQueue: Queue<Node> = LinkedList()
25-
2623
bfsQueue.offer(root)
2724
root.next = null
28-
2925
var temp: Node?
3026
var prev: Node?
31-
3227
while (!bfsQueue.isEmpty()) {
33-
3428
val size = bfsQueue.size
3529
prev = null
36-
3730
for (j in 0 until size) {
38-
3931
temp = bfsQueue.poll()
4032
if (prev != null) prev.next = temp
4133
if (temp!!.left != null) bfsQueue.offer(temp.left)
4234
if (temp.right != null) bfsQueue.offer(temp.right)
4335
prev = temp
4436
}
4537
}
46-
4738
return root
4839
}
4940
}

src.save/main/kotlin/g0101_0200/s0120_triangle/Solution.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package g0101_0200.s0120_triangle
77
import java.util.Arrays
88

99
class Solution {
10-
fun minimumTotal(triangle: List<List<Int>>?): Int {
11-
if (triangle == null || triangle.isEmpty()) {
10+
fun minimumTotal(triangle: List<List<Int>>): Int {
11+
if (triangle.isEmpty()) {
1212
return 0
1313
}
1414
val dp = Array(triangle.size) { IntArray(triangle[triangle.size - 1].size) }

src.save/main/kotlin/g0101_0200/s0126_word_ladder_ii/Solution.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import java.util.LinkedList
88
import java.util.Queue
99

1010
class Solution {
11-
fun findLadders(beginWord: String, endWord: String, wordList: List<String>?): List<List<String?>> {
12-
val ans: MutableList<List<String?>> = ArrayList()
11+
fun findLadders(beginWord: String, endWord: String, wordList: List<String>): List<List<String>> {
12+
val ans: MutableList<List<String>> = ArrayList()
1313
// reverse graph start from endWord
1414
val reverse: MutableMap<String, MutableSet<String>> = HashMap()
1515
// remove the duplicate words
@@ -59,7 +59,7 @@ class Solution {
5959
if (!findEnd) {
6060
return ans
6161
}
62-
val path: MutableSet<String?> = LinkedHashSet()
62+
val path: MutableSet<String> = LinkedHashSet()
6363
path.add(endWord)
6464
// traverse reverse graph from endWord to beginWord
6565
findPath(endWord, beginWord, reverse, ans, path)
@@ -70,14 +70,14 @@ class Solution {
7070
endWord: String,
7171
beginWord: String,
7272
graph: Map<String, MutableSet<String>>,
73-
ans: MutableList<List<String?>>,
74-
path: MutableSet<String?>
73+
ans: MutableList<List<String>>,
74+
path: MutableSet<String>
7575
) {
7676
val next = graph[endWord] ?: return
7777
for (word in next) {
7878
path.add(word)
7979
if (beginWord == word) {
80-
val shortestPath: List<String?> = ArrayList(path)
80+
val shortestPath: List<String> = ArrayList(path)
8181
// reverse words in shortest path
8282
Collections.reverse(shortestPath)
8383
// add the shortest path to ans.

0 commit comments

Comments
 (0)