Skip to content

Commit 3768280

Browse files
committed
solve problem Merge Sorted Array
1 parent 7b1d843 commit 3768280

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ All solutions will be accepted!
6969
|566|[Reshape The Matrix](https://leetcode-cn.com/problems/reshape-the-matrix/description/)|[java/py/js](./algorithms/ReshapeTheMatrix)|Easy|
7070
|575|[Distribute Candies](https://leetcode-cn.com/problems/distribute-candies/description/)|[java/py/js](./algorithms/DistributeCandies)|Easy|
7171
|21|[Merge Two Sorted Lists](https://leetcode-cn.com/problems/merge-two-sorted-lists/description/)|[java/py/js](./algorithms/MergeTwoSortedLists)|Easy|
72+
|88|[Merge Sorted Array](https://leetcode-cn.com/problems/merge-sorted-array/description/)|[java/py/js](./algorithms/MergeSortedArray)|Easy|
7273

7374
# Database
7475
|#|Title|Solution|Difficulty|

algorithms/MergeSortedArray/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Merge Sorted Array
2+
This problem is easy to solve by two linked list
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public void merge(int[] nums1, int m, int[] nums2, int n) {
3+
int[] nums1Copy = new int[m];
4+
System.arraycopy(nums1, 0, nums1Copy, 0, m);
5+
6+
int i = 0, j = 0;
7+
while (i < m && j < n) {
8+
nums1[i + j] = Math.min(nums1Copy[i], nums2[j]);
9+
if (nums1Copy[i] <= nums2[j]) i++;
10+
else j++;
11+
}
12+
13+
while (i < m) {
14+
nums1[i + j] = nums1Copy[i];
15+
i++;
16+
}
17+
18+
while (j < n) {
19+
nums1[i + j] = nums2[j];
20+
j++;
21+
}
22+
}
23+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number} m
4+
* @param {number[]} nums2
5+
* @param {number} n
6+
* @return {void} Do not return anything, modify nums1 in-place instead.
7+
*/
8+
var merge = function(nums1, m, nums2, n) {
9+
let nums1Copy = []
10+
for (let i = 0; i < m; i++) {
11+
nums1Copy.push(nums1[i])
12+
}
13+
14+
let i = j = 0
15+
16+
while (i < m && j < n) {
17+
nums1[i + j] = Math.min(nums1Copy[i], nums2[j])
18+
if (nums1Copy[i] <= nums2[j]) i++
19+
else j++
20+
}
21+
22+
while (i < m) {
23+
nums1[i + j] = nums1Copy[i]
24+
i++
25+
}
26+
27+
while (j < n) {
28+
nums1[i + j] = nums2[j]
29+
j++
30+
}
31+
};
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def merge(self, nums1, m, nums2, n):
3+
"""
4+
:type nums1: List[int]
5+
:type m: int
6+
:type nums2: List[int]
7+
:type n: int
8+
:rtype: void Do not return anything, modify nums1 in-place instead.
9+
"""
10+
nums1_copy = []
11+
for i in range(0, m):
12+
nums1_copy.append(nums1[i])
13+
14+
i = 0
15+
j = 0
16+
while i < m and j < n:
17+
nums1[i + j] = min(nums1_copy[i], nums2[j])
18+
if nums1_copy[i] <= nums2[j]:
19+
i += 1
20+
else:
21+
j += 1
22+
23+
while i < m:
24+
nums1[i + j] = nums1_copy[i]
25+
i += 1
26+
while j < n:
27+
nums1[i + j] = nums2[j]
28+
j += 1
29+

0 commit comments

Comments
 (0)