|
1 | 1 | package LeetCodeJava.Sort;
|
2 | 2 |
|
3 |
| -// https://leetcode.com/problems/merge-sorted-array/ |
4 |
| - |
5 | 3 | import java.util.Arrays;
|
6 | 4 |
|
| 5 | +// https://leetcode.com/problems/merge-sorted-array/ |
| 6 | +/** |
| 7 | + * 88. Merge Sorted Array |
| 8 | + * Solved |
| 9 | + * Easy |
| 10 | + * Topics |
| 11 | + * Companies |
| 12 | + * Hint |
| 13 | + * 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. |
| 14 | + * |
| 15 | + * Merge nums1 and nums2 into a single array sorted in non-decreasing order. |
| 16 | + * |
| 17 | + * 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. |
| 18 | + * |
| 19 | + * |
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * |
| 23 | + * Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 |
| 24 | + * Output: [1,2,2,3,5,6] |
| 25 | + * Explanation: The arrays we are merging are [1,2,3] and [2,5,6]. |
| 26 | + * The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1. |
| 27 | + * Example 2: |
| 28 | + * |
| 29 | + * Input: nums1 = [1], m = 1, nums2 = [], n = 0 |
| 30 | + * Output: [1] |
| 31 | + * Explanation: The arrays we are merging are [1] and []. |
| 32 | + * The result of the merge is [1]. |
| 33 | + * Example 3: |
| 34 | + * |
| 35 | + * Input: nums1 = [0], m = 0, nums2 = [1], n = 1 |
| 36 | + * Output: [1] |
| 37 | + * Explanation: The arrays we are merging are [] and [1]. |
| 38 | + * The result of the merge is [1]. |
| 39 | + * Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1. |
| 40 | + * |
| 41 | + * |
| 42 | + * Constraints: |
| 43 | + * |
| 44 | + * nums1.length == m + n |
| 45 | + * nums2.length == n |
| 46 | + * 0 <= m, n <= 200 |
| 47 | + * 1 <= m + n <= 200 |
| 48 | + * -109 <= nums1[i], nums2[j] <= 109 |
| 49 | + * |
| 50 | + * |
| 51 | + * Follow up: Can you come up with an algorithm that runs in O(m + n) time? |
| 52 | + * |
| 53 | + * |
| 54 | + */ |
7 | 55 | public class MergeSortedArray {
|
8 | 56 |
|
9 | 57 | // V0
|
| 58 | + // IDEA: ARRAY OP + SORT |
| 59 | + public void merge(int[] nums1, int m, int[] nums2, int n) { |
| 60 | + /** |
| 61 | + * NOTE !!! we loop from 0 to `n` |
| 62 | + */ |
| 63 | + for (int i = 0; i < n; i++) { |
| 64 | + /** |
| 65 | + * NOTE !!! we assign nums2[i] val to `i+m` idx at nums1 (nums1[i + m] ) |
| 66 | + */ |
| 67 | + nums1[i + m] = nums2[i]; |
| 68 | + } |
| 69 | + // NOTE: we sort merged array |
| 70 | + Arrays.sort(nums1); |
| 71 | + } |
10 | 72 |
|
11 | 73 | // V1
|
12 | 74 | // IDEA : MERGE AND SORT
|
13 | 75 | // https://leetcode.com/problems/merge-sorted-array/editorial/
|
| 76 | + /** |
| 77 | + * NOTE !!! |
| 78 | + * |
| 79 | + * two integers m and n, |
| 80 | + * representing the number of elements in nums1 and nums2 respectively. |
| 81 | + * |
| 82 | + * -> m : nums1 element cnt |
| 83 | + * -> n : nums2 element cnt |
| 84 | + */ |
14 | 85 | public void merge_2(int[] nums1, int m, int[] nums2, int n) {
|
15 | 86 | for (int i = 0; i < n; i++) {
|
16 | 87 | nums1[i + m] = nums2[i];
|
|
0 commit comments