题目:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
要求将两个有序的排序合并成一个有序的数组
思路:将数组nums1和nums2同时从后往前扫,每次迭代中nums1和nums2中较大的一个元素加入结果数组中,然后对应的数组索引减1,另外一个数组不动。为了防止覆盖nums1的前面元素,结果数组也从后往前扫。
代码AC:
时间复杂度为O(m+n)