Skip to content

Commit f185365

Browse files
committed
Add solution #88
1 parent 22d07ed commit f185365

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
58|[Length of Last Word](./0058-length-of-last-word.js)|Easy|
2929
66|[Plus One](./0066-plus-one.js)|Easy|
3030
67|[Add Binary](./0067-add-binary.js)|Easy|
31+
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
3132
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
3233
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
3334
217|[Contains Duplicate](./0217-contains-duplicate.js)|Easy|

solutions/0088-merge-sorted-array.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 88. Merge Sorted Array
3+
* https://leetcode.com/problems/merge-sorted-array/
4+
* Difficulty: Easy
5+
*
6+
* You are given two integer arrays `nums1` and `nums2`, sorted in non-decreasing order,
7+
* and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2`
8+
* respectively.
9+
*
10+
* Merge `nums1` and `nums2` into a single array sorted in non-decreasing order.
11+
*
12+
* The final sorted array should not be returned by the function, but instead be
13+
* stored inside the array `nums1`. To accommodate this, `nums1` has a length of `m + n`,
14+
* where the first `m` elements denote the elements that should be merged, and the
15+
* last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums1
20+
* @param {number} m
21+
* @param {number[]} nums2
22+
* @param {number} n
23+
* @return {void} Do not return anything, modify nums1 in-place instead.
24+
*/
25+
var merge = function(nums1, m, nums2, n) {
26+
let i = m + n - 1;
27+
28+
m--;
29+
n--;
30+
31+
while (n >= 0) {
32+
nums1[i--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--];
33+
}
34+
};

0 commit comments

Comments
 (0)