문제
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.
주어진 배열 nums1과 nums2는 순서대로 정렬되어있으며, Integer m과 n은 각각 nums1과 nums2가 가진 element의 수를 의미한다.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
nums1과 nums2를 합치고 오름차순으로 정렬하라.
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 + n이며, nums1[m]부터 n까지는 0으로 이루어져있어 이를 무시하고 길이가 n인 nums2을 nums1에 병합해야한다

풀이
var merge = function(nums1, m, nums2, n) {
nums1.splice(m, n);
nums1.push(...nums2);
nums1.sort((a, b) => a - b);
};
Runtime: 49 ms
Memory Usage: 42.3 MB

'Algorithm' 카테고리의 다른 글
| [LeetCode] Count Primes (0) | 2023.09.13 |
|---|---|
| [LeetCode] First Bad Version - 이진탐색 (binary search) (0) | 2023.09.12 |
| [LeetCode] Delete Node in a Linked List (js/연결 리스트) (0) | 2023.09.11 |
| [LeetCode] Reverse String (0) | 2023.09.08 |
| [LeetCode] Single Number (Map()을 활용해 풀기) (0) | 2023.09.07 |