214. Merge Sorted Array

Easy
Reverse Two Pointers
Array

Problem

Given two sorted integer arrays nums1 and nums2, return a single sorted array containing all elements from both arrays.

Examples

Example 1

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

Example 2

Input: [1], []
Output: [1]

Example 3

Input: [], [1]
Output: [1]
Constraints

0 <= nums1.length <= 100000

0 <= nums2.length <= 100000

Both input arrays are sorted in non-decreasing order.

Hints

?? Use one pointer for each input array.

?? Append the smaller current element.

Expected Complexity
Time: O(n + m)
Space: O(n + m)
Follow-up

How would you solve the classic in-place version where nums1 has extra capacity?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,2,3], [2,5,6]
Expected
[1,2,2,3,5,6]