-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortedArray.java
More file actions
48 lines (42 loc) · 1.42 KB
/
MergeSortedArray.java
File metadata and controls
48 lines (42 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package solutions;
import java.util.Arrays;
// [Problem] https://leetcode.com/problems/merge-sorted-array
class MergeSortedArray {
// Two pointers from the beginning
// O(m + n) time, O(m) space
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] nums1Copy = Arrays.copyOf(nums1, m);
int p1 = 0, p2 = 0;
for (int i = 0; i < m + n; i++) {
if (p1 >= m || (p2 < n && nums1Copy[p1] > nums2[p2])) {
nums1[i] = nums2[p2++];
} else {
nums1[i] = nums1Copy[p1++];
}
}
}
// Two pointers from the end
// O(m + n) time, O(1) space
public void mergeFromEnd(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1, p2 = n - 1;
for (int i = m + n - 1; i >= 0; i--) {
if (p2 < 0) {
break;
}
if (p1 >= 0 && nums1[p1] > nums2[p2]) {
nums1[i] = nums1[p1--];
} else {
nums1[i] = nums2[p2--];
}
}
}
// Test
public static void main(String[] args) {
MergeSortedArray solution = new MergeSortedArray();
int[] nums1 = {1, 2, 3, 0, 0, 0}, nums2 = {2, 5, 6};
int m = 3, n = 3;
int[] expectedOutput = {1, 2, 2, 3, 5, 6};
solution.merge(nums1, m, nums2, n);
System.out.println("Test passed? " + Arrays.equals(expectedOutput, nums1));
}
}