|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/**Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. |
4 |
| -
|
5 |
| - Note: |
6 |
| - You may assume that nums1 has enough space (size that is greater or equal to m + n) |
7 |
| - to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.*/ |
| 3 | +/** |
| 4 | + * 88. Merge Sorted Array |
| 5 | + * |
| 6 | + * Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. |
| 7 | + * |
| 8 | + * Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold |
| 9 | + * additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n |
| 10 | + * respectively. |
| 11 | + */ |
8 | 12 |
|
9 | 13 | public class _88 {
|
10 | 14 |
|
11 |
| - //credit:https://discuss.leetcode.com/topic/2461/this-is-my-ac-code-may-help-you |
12 |
| - public static void merge_O1_space(int[] nums1, int m, int[] nums2, int n) { |
13 |
| - int i = m - 1; |
14 |
| - int j = n - 1; |
15 |
| - int k = m + n - 1; |
16 |
| - while (i >= 0 && j >= 0) { |
17 |
| - if (nums1[i] > nums2[j]) { |
18 |
| - nums1[k--] = nums1[i--]; |
19 |
| - } else { |
20 |
| - nums1[k--] = nums2[j--]; |
21 |
| - } |
22 |
| - } |
23 |
| - while (j >= 0) { |
24 |
| - nums1[k--] = nums2[j--]; |
25 |
| - } |
26 |
| - } |
27 |
| - |
28 |
| - /** |
29 |
| - * I used O(m) extra space to create a temp array, but this could be optimized. |
30 |
| - */ |
31 |
| - public static void merge(int[] nums1, int m, int[] nums2, int n) { |
32 |
| - int[] temp = new int[m]; |
33 |
| - for (int i = 0; i < m; i++) { |
34 |
| - temp[i] = nums1[i]; |
| 15 | + public static class Solution1 { |
| 16 | + public void merge(int[] nums1, int m, int[] nums2, int n) { |
| 17 | + int i = m - 1; |
| 18 | + int j = n - 1; |
| 19 | + int k = m + n - 1; |
| 20 | + while (i >= 0 && j >= 0) { |
| 21 | + if (nums1[i] > nums2[j]) { |
| 22 | + nums1[k--] = nums1[i--]; |
| 23 | + } else { |
| 24 | + nums1[k--] = nums2[j--]; |
35 | 25 | }
|
36 |
| - for (int i = 0, j = 0, k = 0; i < m || j < n; ) { |
37 |
| - if (i == m) { |
38 |
| - for (; j < n; ) { |
39 |
| - nums1[k++] = nums2[j++]; |
40 |
| - } |
41 |
| - break; |
42 |
| - } |
43 |
| - if (j == n) { |
44 |
| - for (; i < m; ) { |
45 |
| - nums1[k++] = temp[i++]; |
46 |
| - } |
47 |
| - break; |
48 |
| - } |
49 |
| - |
50 |
| - if (temp[i] > nums2[j]) { |
51 |
| - nums1[k++] = nums2[j++]; |
52 |
| - } else { |
53 |
| - nums1[k++] = temp[i++]; |
54 |
| - } |
55 |
| - } |
56 |
| - } |
57 |
| - |
58 |
| - public static void main(String... args) { |
59 |
| -// int[] nums1 = new int[]{2,0}; |
60 |
| -// int m = 1; |
61 |
| -// int[] nums2 = new int[]{1}; |
62 |
| -// int n = 1; |
63 |
| - |
64 |
| - int[] nums1 = new int[]{4, 5, 6, 0, 0, 0}; |
65 |
| - int m = 3; |
66 |
| - int[] nums2 = new int[]{1, 2, 3}; |
67 |
| - int n = 3; |
68 |
| - merge(nums1, m, nums2, n); |
| 26 | + } |
| 27 | + while (j >= 0) { |
| 28 | + nums1[k--] = nums2[j--]; |
| 29 | + } |
69 | 30 | }
|
| 31 | + } |
70 | 32 | }
|
0 commit comments