November 4, 2023

Viiisit [LeetCode] - 88. Merge Sorted Array

#javascript#ruby

keep learning, keep coding!

Problem - Merge Sorted Array

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

翻譯蒟蒻

最終的 nums1 陣列中應該包含 m 個 nums1 的元素和 n 個 nums2 的元素
(nums1 長度為 m + n),且順序由小到大,
可以發現 nums2 的元素數量就等於 nums1 裡面 0 的數量。


Solution - JavaScript

Solution 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {number[]} nums1
* @param {number} m
* @param {number[]} nums2
* @param {number} n
* @return {void} Do not return anything, modify nums1 in-place instead.
*/
var merge = function(nums1, m, nums2, n) {
for (let i = m; i < m + n; i++) {
nums1[i] = nums2[i - m]
}
nums1.sort((a, b) => a-b )
};

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {number[]} nums1
* @param {number} m
* @param {number[]} nums2
* @param {number} n
* @return {void} Do not return anything, modify nums1 in-place instead.
*/
var merge = function(nums1, m, nums2, n) {
for (let i = 0; i < n; i++) {
nums1[m + i] = nums2[i]
}
nums1.sort((a, b) => a-b )
};

Solution - Ruby

1
2
3
4
5
6
7
8
9
10
11
# @param {Integer[]} nums1
# @param {Integer} m
# @param {Integer[]} nums2
# @param {Integer} n
# @return {Void} Do not return anything, modify nums1 in-place instead.
def merge(nums1, m, nums2, n)
(0...n).each do |i|
nums1[m + i] = nums2[i]
end
nums1.sort!
end

LeetCode 傳送門 - Merge Sorted Array