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.
Example
1 | Input: |
为什么反过来?
- 因为
nums1
后面是空的,说明后面都是0,所以反过来赋值不会覆盖原来的自身的值,否则覆盖。 - 最后的while,因为如果
nums2
不是空,就还要赋值;而如果是因为nums1
不要紧,因为本身自己的值就在里面
1 | public void merge(int[] nums1, int m, int[] nums2, int n) { |