Skip to content

Commit

Permalink
Update 0088-Merge-Sorted-Array.md
Browse files Browse the repository at this point in the history
添加C++、Java、Python代码
  • Loading branch information
ztianming authored Sep 30, 2020
1 parent 5734003 commit 5cab225
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion 0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,94 @@ nums2 = [2,5,6], n = 3
<img src="../Animation/Animation.gif" alt="Animation" style="zoom:150%;" />

### 参考代码
C++ Code:

```c++
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i=m-1, j=n-1, k=m+n-1;
// 合并
while(i>=0 && j>=0)
{
if(nums1[i] > nums2[j])
{
nums1[k--] = nums1[i--];
}
else
{
nums1[k--] = nums2[j--];
}
}
// 合并剩余的nums2
while(j>=0)
{
nums1[k--] = nums2[j--];
}
}
};
```

Java Code:

```java
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i=m-1, j=n-1, k=m+n-1;
// 合并
while(i>=0 && j>=0)
{
if(nums1[i] > nums2[j])
{
nums1[k--] = nums1[i--];
}
else
{
nums1[k--] = nums2[j--];
}
}
// 合并剩余的nums2
while(j>=0)
{
nums1[k--] = nums2[j--];
}
}
}
```

Python Code:

```python
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
i,j,k = m-1, n-1, m+n-1

while i >= 0 and j >= 0:
# print(i,j,k, nums1)
# print(nums1[i], nums2[j])
if nums1[i] > nums2[j]:
nums1[k] = nums1[i]
k-=1
i-=1
else:
nums1[k] = nums2[j]
k-=1
j-=1
while j >= 0:
nums1[k] = nums2[j]
k-=1
j-=1

```

JavaScript Code:

```javascript
/**
Expand Down Expand Up @@ -105,4 +193,4 @@ var merge = function(nums1, m, nums2, n) {



![](../../Pictures/qrcode.jpg)
![](../../Pictures/qrcode.jpg)

0 comments on commit 5cab225

Please sign in to comment.