Skip to content

Commit ba6e3a7

Browse files
committed
feat: add the solution of Merge Sorted Array(088) with kotlin.
1 parent 6f9f6b5 commit ba6e3a7

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
| [069][069-question] | [Sqrt(x)][069-tips] | [][069-java] | | [][069-kotlin] |
4747
| [070][070-question] | [Climbing Stairs][070-tips] | [][070-java] | | [][070-kotlin] |
4848
| [083][083-question] | [Remove Duplicates from Sorted List][083-tips] | [][083-java] | | [][083-kotlin] |
49-
| [088][088-question] | [Merge Sorted Array][088-tips] | [][088-java] | | |
49+
| [088][088-question] | [Merge Sorted Array][088-tips] | [][088-java] | | [][088-kotlin] |
5050
| [100][100-question] | [Same Tree][100-tips] | [][100-java] | | |
5151
| [101][101-question] | [Symmetric Tree][101-tips] | [][101-java] | | |
5252
| [104][104-question] | [Maximum Depth of Binary Tree][104-tips] | [][104-java] | | |
@@ -493,4 +493,5 @@
493493
[069-kotlin]: ./src/_069/kotlin/Solution.kt
494494
[070-kotlin]: ./src/_070/kotlin/Solution.kt
495495
[083-kotlin]: ./src/_083/kotlin/Solution.kt
496+
[088-kotlin]: ./src/_088/kotlin/Solution.kt
496497
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_088/kotlin/Solution.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _088.kotlin
2+
3+
import java.util.*
4+
5+
/**
6+
* @author relish
7+
* @since 2018/04/24
8+
*/
9+
class Solution {
10+
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int): Unit {
11+
var index = n + m - 1
12+
var i = m - 1
13+
var j = n - 1
14+
15+
while ((i >= 0 || j >= 0) && index >= 0) {
16+
if (j < 0 && i in 0..(m - 1)) {
17+
nums1[index--] = nums1[i--]
18+
continue
19+
}
20+
if (i < 0 && j in 0..(n - 1)) {
21+
nums1[index--] = nums2[j--]
22+
continue
23+
}
24+
if (nums1[i] > nums2[j]) {
25+
nums1[index--] = nums1[i--]
26+
} else {
27+
nums1[index--] = nums2[j--]
28+
}
29+
}
30+
}
31+
}
32+
33+
fun main(args: Array<String>) {
34+
val arr1 = intArrayOf(1, 2, 3, 0, 0, 0)
35+
val arr2 = intArrayOf(2, 5, 6)
36+
Solution().merge(arr1, 3, arr2, 3)
37+
println(Arrays.toString(arr1))
38+
}

tips/088/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ You may assume that *nums1* has enough space (size that is greater or equal to *
1515

1616
题意是给两个已排序的数组 `nums1``nums2`,合并 `nums2``nums1` 中,两数组元素个数分别为 `m``n`,而且 `nums1` 数组的长度足够容纳 `m + n` 个元素,如果我们按顺序排下去,那肯定要开辟一个新数组来保存元素,如果我们选择逆序,这样利用 `nums1` 自身空间足矣,不会出现覆盖的情况,依次把大的元素插入到 `nums1` 的末尾,确保 `nums2` 中的元素全部插入到 `nums1` 即可。
1717

18+
Java:
1819
```java
1920
class Solution {
2021
public void merge(int[] nums1, int m, int[] nums2, int n) {
@@ -27,6 +28,32 @@ class Solution {
2728
}
2829
```
2930

31+
kotlin(208ms/100.00%)
32+
```kotlin
33+
class Solution {
34+
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int): Unit {
35+
var index = n + m - 1
36+
var i = m - 1
37+
var j = n - 1
38+
39+
while ((i >= 0 || j >= 0) && index >= 0) {
40+
if (j < 0 && i in 0..(m - 1)) {
41+
nums1[index--] = nums1[i--]
42+
continue
43+
}
44+
if (i < 0 && j in 0..(n - 1)) {
45+
nums1[index--] = nums2[j--]
46+
continue
47+
}
48+
if (nums1[i] > nums2[j]) {
49+
nums1[index--] = nums1[i--]
50+
} else {
51+
nums1[index--] = nums2[j--]
52+
}
53+
}
54+
}
55+
}
56+
```
3057

3158
## 结语
3259

0 commit comments

Comments
 (0)