# 88. Merge Sorted Array

{% embed url="<https://leetcode.com/problems/merge-sorted-array/>" %}

简单`merge sort`，硬是搞这个奇怪的 in-place 弄麻烦了点，既然`nums1`空出来的位置是在右侧，那就从右侧开始 merge，指针移动思路有点像Q283。

{% content-ref url="283.-move-zeroes" %}
[283.-move-zeroes](https://xuqiangwen1994.gitbook.io/practice/283.-move-zeroes)
{% endcontent-ref %}

```python
class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        i = len(nums1)-1
        while m > 0 or n > 0:
            if m <= 0:
                nums1[i] = nums2[n-1]
                n -= 1
            elif n <= 0:
                nums1[i] = nums1[m-1]
                m -= 1
            elif nums1[m-1] >= nums2[n-1]:
                nums1[i] = nums1[m-1]
                m -= 1
            else:
                nums1[i] = nums2[n-1]
                n -=1
            i -= 1
```
