-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path88Solution.java
47 lines (46 loc) · 1.5 KB
/
88Solution.java
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
//version1: space O(m + n) time o(n)
public void merge(int[] nums1, int m, int[] nums2, int n) {
// merge two sorted array to one array
//init an array size m+n
int[] newArray = new int[m + n];
// compare put smaller into new array
int count = 0;
int i = 0;
int j = 0;
while(i < m && j < n) {
if (nums1[i] < nums2[j]) {
newArray[count++] = nums1[i++];
} else{
newArray[count++] = nums2[j++];
}
}
while (i < m) {
newArray[count++] = nums1[i++];
}
while (j < n) {
newArray[count++] = nums2[j++];
}
// copy to original one
for (int k = 0; k < newArray.length; k++) {
nums1[k] = newArray[k];
}
}
// version2: space O(m) time O(n)
// The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arrayCopy copies into an existing array.
// If we read the source code of Arrays.copyOf(), we can see that it uses System.arraycopy().
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = 0;
int j = 0;
while (i < m && j < n) {
if (nums1[i] > nums2[j]) {
System.arrayCopy(nums1, i , nums1, i + 1, nums1.length - i);
nums1[i] = nums2[j];
j++;
} else {
i++;
}
}
while (j < n) {
System.arrayCopy(nums2, j, nums1, i, nums2.length - j);
}
}