Skip to content

Commit 72836a2

Browse files
authored
Added tasks 107-115.
1 parent f95f180 commit 72836a2

File tree

12 files changed

+883
-199
lines changed

12 files changed

+883
-199
lines changed

README.md

Lines changed: 181 additions & 164 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0001_0100/s0018_4sum/readme.md

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,57 @@ You may return the answer in **any order**.
3737
import java.util.Arrays
3838

3939
class Solution {
40-
fun fourSum(nums: IntArray, target: Int): List<List<Int?>?> {
41-
var list: MutableList<List<Int?>?> = ArrayList()
42-
var j: Int
43-
var k: Int
44-
var l: Int
40+
fun fourSum(nums: IntArray, target: Int): List<List<Int>> {
41+
val ret: MutableList<List<Int>> = ArrayList()
42+
if (nums.size < 4) {
43+
return ret
44+
}
45+
if (nums[0] == 1000000000 && nums[1] == 1000000000) {
46+
return ret
47+
}
4548
Arrays.sort(nums)
46-
var i: Int = 0
47-
while (i < nums.size - 3) {
48-
if (i > 0 && nums[i] == nums[i - 1]) {
49-
i++
49+
for (i in 0 until nums.size - 3) {
50+
if (i != 0 && nums[i] == nums[i - 1]) {
5051
continue
5152
}
52-
j = i + 1
53-
while (j < nums.size - 2) {
54-
if (j > i + 1 && nums[j] == nums[j - 1]) {
55-
j++
53+
for (j in i + 1 until nums.size - 2) {
54+
if (j != i + 1 && nums[j] == nums[j - 1]) {
5655
continue
5756
}
58-
k = j + 1
59-
l = nums.size - 1
60-
while (k < l) {
61-
val sum = nums[i] + nums[j] + nums[k] + nums[l]
57+
var left = j + 1
58+
var right = nums.size - 1
59+
val half = nums[i] + nums[j]
60+
if (half + nums[left] + nums[left + 1] > target) {
61+
continue
62+
}
63+
if (half + nums[right] + nums[right - 1] < target) {
64+
continue
65+
}
66+
while (left < right) {
67+
val sum = nums[left] + nums[right] + half
6268
if (sum == target) {
63-
val l1 = ArrayList<Int?>()
64-
l1.add(nums[i])
65-
l1.add(nums[j])
66-
l1.add(nums[k])
67-
l1.add(nums[l])
68-
list.add(l1)
69-
l--
70-
if (k < l && nums[l] == nums[l + 1]) {
71-
l--
69+
ret.add(listOf(nums[left++], nums[right--], nums[i], nums[j]))
70+
while (nums[left] == nums[left - 1] && left < right) {
71+
left++
72+
}
73+
while (nums[right] == nums[right + 1] && left < right) {
74+
right--
75+
}
76+
} else if (sum < target) {
77+
left++
78+
while (nums[left] == nums[left - 1] && left < right) {
79+
left++
7280
}
73-
} else if (sum > target) {
74-
l--
7581
} else {
76-
k++
82+
right--
83+
while (nums[right] == nums[right + 1] && left < right) {
84+
right--
85+
}
7786
}
7887
}
79-
j++
8088
}
81-
i++
8289
}
83-
list = ArrayList(LinkedHashSet(list))
84-
return list
90+
return ret
8591
}
8692
}
8793
```

src/main/kotlin/g0001_0100/s0043_multiply_strings/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class Solution {
4747
chars.append(res[index].toString())
4848
index++
4949
}
50-
System.out.println(chars)
5150
return chars.toString()
5251
}
5352

src/main/kotlin/g0001_0100/s0100_same_tree/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import com_github_leetcode.TreeNode
5656
class Solution {
5757
private fun trav(n: TreeNode?, m: TreeNode?): Boolean {
5858
return if (n != null && m != null) {
59-
if (n.`val` !== m.`val`) {
59+
if (n.`val` != m.`val`) {
6060
false
6161
} else trav(n.left, m.left) && trav(n.right, m.right)
6262
} else {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 107\. Binary Tree Level Order Traversal II
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
13+
14+
**Input:** root = [3,9,20,null,null,15,7]
15+
16+
**Output:** [[15,7],[9,20],[3]]
17+
18+
**Example 2:**
19+
20+
**Input:** root = [1]
21+
22+
**Output:** [[1]]
23+
24+
**Example 3:**
25+
26+
**Input:** root = []
27+
28+
**Output:** []
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the tree is in the range `[0, 2000]`.
33+
* `-1000 <= Node.val <= 1000`
34+
35+
## Solution
36+
37+
```kotlin
38+
import com_github_leetcode.TreeNode
39+
import java.util.Collections
40+
import kotlin.collections.ArrayList
41+
42+
/**
43+
* Example:
44+
* var ti = TreeNode(5)
45+
* var v = ti.`val`
46+
* Definition for a binary tree node.
47+
* class TreeNode(var `val`: Int) {
48+
* var left: TreeNode? = null
49+
* var right: TreeNode? = null
50+
* }
51+
*/
52+
class Solution {
53+
private val order: MutableList<MutableList<Int>> = ArrayList()
54+
fun levelOrderBottom(root: TreeNode?): List<MutableList<Int>> {
55+
getOrder(root, 0)
56+
Collections.reverse(order)
57+
return order
58+
}
59+
60+
fun getOrder(root: TreeNode?, level: Int) {
61+
if (root != null) {
62+
if (level + 1 > order.size) {
63+
order.add(ArrayList())
64+
}
65+
order[level].add(root.`val`)
66+
getOrder(root.left, level + 1)
67+
getOrder(root.right, level + 1)
68+
}
69+
}
70+
}
71+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 108\. Convert Sorted Array to Binary Search Tree
5+
6+
Easy
7+
8+
Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a **height-balanced** binary search tree_.
9+
10+
A **height-balanced** binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg)
15+
16+
**Input:** nums = [-10,-3,0,5,9]
17+
18+
**Output:** [0,-3,9,-10,null,5]
19+
20+
**Explanation:** [0,-10,5,null,-3,null,9] is also accepted: ![](https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg)
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2021/02/18/btree.jpg)
25+
26+
**Input:** nums = [1,3]
27+
28+
**Output:** [3,1]
29+
30+
**Explanation:** [1,null,3] and [3,1] are both height-balanced BSTs.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
35+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
36+
* `nums` is sorted in a **strictly increasing** order.
37+
38+
## Solution
39+
40+
```kotlin
41+
import com_github_leetcode.TreeNode
42+
43+
/**
44+
* Example:
45+
* var ti = TreeNode(5)
46+
* var v = ti.`val`
47+
* Definition for a binary tree node.
48+
* class TreeNode(var `val`: Int) {
49+
* var left: TreeNode? = null
50+
* var right: TreeNode? = null
51+
* }
52+
*/
53+
class Solution {
54+
fun sortedArrayToBST(num: IntArray): TreeNode? {
55+
/*1. Set up recursion*/
56+
return makeTree(num, 0, num.size - 1)
57+
}
58+
59+
private fun makeTree(num: IntArray, left: Int, right: Int): TreeNode? {
60+
/*2. left as lowest# can't be greater than right*/
61+
if (left > right) {
62+
return null
63+
}
64+
/*3. Set median# as node*/
65+
val mid = (left + right) / 2
66+
val midNode = TreeNode(num[mid])
67+
/*4. Set mid node's kids*/
68+
midNode.left = makeTree(num, left, mid - 1)
69+
midNode.right = makeTree(num, mid + 1, right)
70+
/*5. Sends node back || Goes back to prev node*/
71+
return midNode
72+
}
73+
}
74+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 109\. Convert Sorted List to Binary Search Tree
5+
6+
Medium
7+
8+
Given the `head` of a singly linked list where elements are **sorted in ascending order**, convert it to a height balanced BST.
9+
10+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of _every_ node never differ by more than 1.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/08/17/linked.jpg)
15+
16+
**Input:** head = [-10,-3,0,5,9]
17+
18+
**Output:** [0,-3,9,-10,null,5]
19+
20+
**Explanation:** One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
21+
22+
**Example 2:**
23+
24+
**Input:** head = []
25+
26+
**Output:** []
27+
28+
**Constraints:**
29+
30+
* The number of nodes in `head` is in the range <code>[0, 2 * 10<sup>4</sup>]</code>.
31+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
32+
33+
## Solution
34+
35+
```kotlin
36+
import com_github_leetcode.ListNode
37+
import com_github_leetcode.TreeNode
38+
39+
/*
40+
* Example:
41+
* var li = ListNode(5)
42+
* var v = li.`val`
43+
* Definition for singly-linked list.
44+
* class ListNode(var `val`: Int) {
45+
* var next: ListNode? = null
46+
* }
47+
*/
48+
/*
49+
* Example:
50+
* var ti = TreeNode(5)
51+
* var v = ti.`val`
52+
* Definition for a binary tree node.
53+
* class TreeNode(var `val`: Int) {
54+
* var left: TreeNode? = null
55+
* var right: TreeNode? = null
56+
* }
57+
*/
58+
class Solution {
59+
fun sortedListToBST(head: ListNode?): TreeNode? {
60+
// Empty list -> empty tree / sub-tree
61+
if (head == null) {
62+
return null
63+
}
64+
// No next node -> this node will become leaf
65+
if (head.next == null) {
66+
val leaf = TreeNode(head.`val`)
67+
leaf.left = null
68+
leaf.right = null
69+
return leaf
70+
}
71+
var slow = head
72+
// Head-Start fast by 1 to get slow = mid -1
73+
var fast = head.next!!.next
74+
// Find the mid of list
75+
while (fast != null && fast.next != null) {
76+
slow = slow!!.next
77+
fast = fast.next!!.next
78+
}
79+
// slow.next -> mid = our "root"
80+
val root = TreeNode(slow!!.next!!.`val`)
81+
// Right sub tree from mid - end
82+
root.right = sortedListToBST(slow.next!!.next)
83+
// Left sub tree from head - mid (chop slow.next)
84+
slow.next = null
85+
root.left = sortedListToBST(head)
86+
return root
87+
}
88+
}
89+
```

0 commit comments

Comments
 (0)