-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution(cpp): 88. Merge Sorted Array
88. Merge Sorted Array
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# [88. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | ||
<div class="xFUwe" data-track-load="description_content"><p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> | ||
|
||
<p><strong>Merge</strong> <code>nums1</code> and <code>nums2</code> into a single array sorted in <strong>non-decreasing order</strong>.</p> | ||
|
||
<p>The final sorted array should not be returned by the function, but instead be <em>stored inside the array </em><code>nums1</code>. To accommodate this, <code>nums1</code> has a length of <code>m + n</code>, where the first <code>m</code> elements denote the elements that should be merged, and the last <code>n</code> elements are set to <code>0</code> and should be ignored. <code>nums2</code> has a length of <code>n</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 | ||
<strong>Output:</strong> [1,2,2,3,5,6] | ||
<strong>Explanation:</strong> The arrays we are merging are [1,2,3] and [2,5,6]. | ||
The result of the merge is [<u>1</u>,<u>2</u>,2,<u>3</u>,5,6] with the underlined elements coming from nums1. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums1 = [1], m = 1, nums2 = [], n = 0 | ||
<strong>Output:</strong> [1] | ||
<strong>Explanation:</strong> The arrays we are merging are [1] and []. | ||
The result of the merge is [1]. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums1 = [0], m = 0, nums2 = [1], n = 1 | ||
<strong>Output:</strong> [1] | ||
<strong>Explanation:</strong> The arrays we are merging are [] and [1]. | ||
The result of the merge is [1]. | ||
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>nums1.length == m + n</code></li> | ||
<li><code>nums2.length == n</code></li> | ||
<li><code>0 <= m, n <= 200</code></li> | ||
<li><code>1 <= m + n <= 200</code></li> | ||
<li><code>-10<sup>9</sup> <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong>Follow up: </strong>Can you come up with an algorithm that runs in <code>O(m + n)</code> time?</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Solution | ||
{ | ||
public: | ||
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n) | ||
{ | ||
int i = m - 1; | ||
int j = n - 1; | ||
int k = m + n - 1; | ||
|
||
while (i >= 0 && j >= 0) | ||
{ | ||
if (nums1[i] > nums2[j]) | ||
{ | ||
nums1[k] = nums1[i]; | ||
i--; | ||
} | ||
else | ||
{ | ||
nums1[k] = nums2[j]; | ||
j--; | ||
} | ||
k--; | ||
} | ||
|
||
while (j >= 0) | ||
{ | ||
nums1[k] = nums2[j]; | ||
k--; | ||
j--; | ||
} | ||
} | ||
}; |