Merge Sorted Array

Given two sorted integer arraysnums1_and_nums2, merge_nums2_into_nums1_as one sorted array.

Note:

  • The number of elements initialized in

    nums1

    and

    nums2

    are

    m

    and

    n

    respectively.

  • You may assume that

    nums1

    has enough space (size that is greater or equal to

    m

    +

    n

    ) to hold additional elements from

    nums2

    .

Example:

Input:

nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3


Output:
 [1,2,2,3,5,6]

分析

尾部开始填充

3个while,开始while i>=0 and j>=0: 然后while i>=0:和while j>=0:

class Solution:
    def merge(self, nums1, m, nums2, n):
        """
        :type nums1: List[int]
        :type m: int
        :type nums2: List[int]
        :type n: int
        :rtype: void Do not return anything, modify nums1 in-place instead.
        """
        if not n:
            return
        i = m-1
        j=n-1
        k=m+n-1
        while i>=0 and j>=0:
            if nums1[i]<nums2[j]:
                nums1[k] = nums2[j]
                k-=1
                j-=1
            else:
                nums1[k] = nums1[i]
                k-=1
                i-=1
        while i>=0:
            nums1[k] = nums1[i]
            k -= 1
            i -= 1
        while j>=0:
            nums1[k] = nums2[j]
            k -= 1
            j -= 1

java 代码简单,m到头证明结束,所以只需要判断N。

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int i=m-1;
		int j=n-1;
		int k = m+n-1;
		while(i >=0 && j>=0)
		{
			if(A[i] > B[j])
				A[k--] = A[i--];
			else
				A[k--] = B[j--];
		}
		while(j>=0)
			A[k--] = B[j--];
    }
};

Last updated