题目来源:https://leetcode.com/problems/merge-sorted-array/
题目难度: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.
Example:
1 | Input: |
解答1[Java]
思路
新建一个数组,然后把待合并的数组的元素依次填充到新数组中。
实现
1 | class Solution { |
复杂度分析
空间复杂度为 $O(m+n)$。
时间复杂度为 $O(m+n)$。
解答2[Java]
思路
既然数组1后边有多余的位置,就从后面开始填充,从后向前,原地合并。
实现
1 | class Solution { |