-
[leetcode] 88. Merge Sorted Array알고리즘 2021. 11. 8. 21:52
Description
nums1 배열과 해당 배열에 유효한 값이 몇개인지를 나타내는 수 m nums2 배열과 해당 배열에 유효한 값이 몇개인지를 나타내는 수 n 위 값들이 매개변수로 들어왔을때 새로운 배열 생성없이 두 배열을 하나의 배열로 합친 후 내림차순으로 정렬하라.
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Success
m 부터 m+n 까지 반복문을 돌려 nums1 배열의 끝 지점에 nums2 의 값이 들어갈 수 있도록 인덱스를 연산한 후 할당
class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { for (int i=m; i<m+n; i++) { nums1[i] = nums2[i-m]; } Arrays.sort(nums1); } }
출처
'알고리즘' 카테고리의 다른 글
[leetcode] Check If N and Its Double Exist (0) 2021.11.12 [leetcode] 27. Remove Element (0) 2021.11.09 [leetcode] 1089. Duplicate Zeros (0) 2021.11.07 [leetcode] 977. Squares of a Sorted Array (0) 2021.11.06 [leetcode] Find Numbers with Even Number of Digits (0) 2021.11.06