A05344c1805d27ac2e90d096e4b16422
(难度Easy)Problem 88. Merge Sorted Array(归并有序数组)

链接

Merge Sorted Array - LeetCode

难度等级

Easy

题干

Given two sorted integer arrays nums1 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.

题意

给定两个有序数组,需要将这两个数组内的元素归并在一起。但是不能申请额外的空间,要写成一个build-in的方法,最后所有的元素全部存在nums1这个vector当中

样例

Example:

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

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

题解

这题相信大多数人一眼就可以看出来是一个归并,但问题是我们不能创建多余的数组,直接归并的话会有点问题。

因为比如说nums1是[1, 2, 7], nums2是[2, 3, 4]

top Created with Sketch.