10000 refactor 88 · tes3906/Leetcode@d4859ff · GitHub
[go: up one dir, main page]

Skip to content

Commit d4859ff

Browse files
refactor 88
1 parent 30bdd4c commit d4859ff

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed
Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,32 @@
11
package com.fishercoder.solutions;
22

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+
*/
812

913
public class _88 {
1014

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--];
3525
}
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+
}
6930
}
31+
}
7032
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._88;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _88Test {
10+
11+
private static _88.Solution1 solution1;
12+
private int[] nums1;
13+
private int[] nums2;
14+
private int[] expected;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _88.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
nums1 = new int[] {2, 0};
24+
nums2 = new int[] {1};
25+
expected = new int[] {1, 2};
26+
solution1.merge(nums1, 1, nums2, 1);
27+
assertArrayEquals(expected, nums1);
28+
}
29+
30+
@Test
31+
public void test2() {
32+
nums1 = new int[] {4, 5, 6, 0, 0, 0};
33+
nums2 = new int[] {1, 2, 3};
34+
expected = new int[] {1, 2, 3, 4, 5, 6};
35+
solution1.merge(nums1, 3, nums2, 3);
36+
assertArrayEquals(expected, nums1);
37+
}
38+
}

0 commit comments

Comments
 (0)
0