728x90
1. 문제
2. 요구사항
1) 배열 nums1과 nums2가 주어지며, num1은 충분한 공간을 할당받고 있다.
2) num1의 초기 크기는 m이고, num2의 크기는 n이다.
3) 두 배열을 합쳐 nums1에 정렬된 형태로 저장해야한다.
3. 핵심 아이디어
1) 뒤에서부터 채우고 정렬
nums1의 유효 인덱스 다음부터 nums2 배열을 삽입한다.
그 후, nums1과 nums2가 합친 배열이 nums1에 저장되는데 이를 sort로 정렬한다.
2) 코드
class Solution {
void merge(List<int> nums1, int m, List<int> nums2, int n) {
// nums1의 채워지지 않은 요소부터 시작
for (int i = m; i < m + n; i++) {
nums1[i] = nums2[i - m];
}
nums1.sort();
}
}
728x90
'Programming > LeetCode' 카테고리의 다른 글
| [Dart] 121. Best Time to Buy and Sell Stock (0) | 2025.03.24 |
|---|---|
| [Dart] 104. Maximum Depth of Binary Tree (0) | 2025.03.21 |
| [Dart] 58. Length of Last Word (0) | 2025.03.19 |
| [Dart] LeetCode 28. Find the Index of the First Occurrence in a String (0) | 2025.03.18 |
| [Dart] LeetCode 27. Remove Element (0) | 2025.03.17 |