Skip to content

Commit 1a3fc30

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0088
See https://leetcode-cn.com/problems/merge-sorted-array/
1 parent 842cfac commit 1a3fc30

File tree

6 files changed

+83
-11
lines changed

6 files changed

+83
-11
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
1. [旋转数组](/solution/0100-0199/0189.Rotate%20Array/README.md)
5555
1. [三数之和](/solution/0000-0099/0015.3Sum/README.md)
5656
1. [四数之和](/solution/0000-0099/0018.4Sum/README.md)
57+
1. [合并两个有序数组](/solution/0000-0099/0088.Merge%20Sorted%20Array/README.md)
5758

5859
### 字符串
5960

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
5353
1. [Rotate Array](/solution/0100-0199/0189.Rotate%20Array/README_EN.md)
5454
1. [3Sum](/solution/0000-0099/0015.3Sum/README_EN.md)
5555
1. [4Sum](/solution/0000-0099/0018.4Sum/README_EN.md)
56+
1. [Merge Sorted Array](/solution/0000-0099/0088.Merge%20Sorted%20Array/README_EN.md)
5657

5758
### Strings
5859

solution/0000-0099/0088.Merge Sorted Array/README.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,50 @@ nums2 = [2,5,6], n = 3
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33+
双指针解决。
34+
3335
<!-- tabs:start -->
3436

3537
### **Python3**
3638

3739
<!-- 这里可写当前语言的特殊实现逻辑 -->
3840

3941
```python
40-
42+
class Solution:
43+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
44+
"""
45+
Do not return anything, modify nums1 in-place instead.
46+
"""
47+
i, j = m - 1, n - 1
48+
k = m + n - 1
49+
while j >= 0:
50+
if i >= 0 and nums1[i] > nums2[j]:
51+
nums1[k] = nums1[i]
52+
i -= 1
53+
else:
54+
nums1[k] = nums2[j]
55+
j -= 1
56+
k -= 1
4157
```
4258

4359
### **Java**
4460

4561
<!-- 这里可写当前语言的特殊实现逻辑 -->
4662

4763
```java
48-
64+
class Solution {
65+
public void merge(int[] nums1, int m, int[] nums2, int n) {
66+
int i = m - 1, j = n - 1;
67+
int k = m + n - 1;
68+
while (j >= 0) {
69+
if (i >= 0 && nums1[i] >= nums2[j]) {
70+
nums1[k--] = nums1[i--];
71+
} else {
72+
nums1[k--] = nums2[j--];
73+
}
74+
}
75+
}
76+
}
4977
```
5078

5179
### **...**

solution/0000-0099/0088.Merge Sorted Array/README_EN.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,39 @@ nums2 = [2,5,6], n = 3
3636
### **Python3**
3737

3838
```python
39-
39+
class Solution:
40+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
41+
"""
42+
Do not return anything, modify nums1 in-place instead.
43+
"""
44+
i, j = m - 1, n - 1
45+
k = m + n - 1
46+
while j >= 0:
47+
if i >= 0 and nums1[i] > nums2[j]:
48+
nums1[k] = nums1[i]
49+
i -= 1
50+
else:
51+
nums1[k] = nums2[j]
52+
j -= 1
53+
k -= 1
4054
```
4155

4256
### **Java**
4357

4458
```java
45-
59+
class Solution {
60+
public void merge(int[] nums1, int m, int[] nums2, int n) {
61+
int i = m - 1, j = n - 1;
62+
int k = m + n - 1;
63+
while (j >= 0) {
64+
if (i >= 0 && nums1[i] >= nums2[j]) {
65+
nums1[k--] = nums1[i--];
66+
} else {
67+
nums1[k--] = nums2[j--];
68+
}
69+
}
70+
}
71+
}
4672
```
4773

4874
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public void merge(int[] nums1, int m, int[] nums2, int n) {
3-
int index = m+n-1;
4-
int index1 = m-1;
5-
int index2 = n-1;
6-
while(index1>=0&&index2>=0){
7-
if (nums1[index1]<nums2[index2]) nums1[index--] = nums2[index2--];
8-
else nums1[index--] = nums1[index1--];
3+
int i = m - 1, j = n - 1;
4+
int k = m + n - 1;
5+
while (j >= 0) {
6+
if (i >= 0 && nums1[i] >= nums2[j]) {
7+
nums1[k--] = nums1[i--];
8+
} else {
9+
nums1[k--] = nums2[j--];
10+
}
911
}
10-
while(index2>=0) nums1[index--] = nums2[index2--];
1112
}
1213
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
3+
"""
4+
Do not return anything, modify nums1 in-place instead.
5+
"""
6+
i, j = m - 1, n - 1
7+
k = m + n - 1
8+
while j >= 0:
9+
if i >= 0 and nums1[i] > nums2[j]:
10+
nums1[k] = nums1[i]
11+
i -= 1
12+
else:
13+
nums1[k] = nums2[j]
14+
j -= 1
15+
k -= 1

0 commit comments

Comments
 (0)