-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_88.java
More file actions
47 lines (39 loc) · 1.13 KB
/
_88.java
File metadata and controls
47 lines (39 loc) · 1.13 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
package leetcode;
import java.util.Arrays;
public class _88 {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] tmp = Arrays.copyOf(nums1, nums1.length);
int i = 0;
int j = 0;
int k = 0;
while (i < m && j < n) {
if (tmp[i] < nums2[j]) {
nums1[k++] = tmp[i++];
} else {
nums1[k++] = nums2[j++];
}
}
if (i == m) {
while (j < n) {
nums1[k++] = nums2[j++];
}
} else {
while (i < m) {
nums1[k++] = tmp[i++];
}
}
}
public static void main(String[] args) {
_88 solution = new _88();
int[] list = new int[] {1, 2, 3, 0, 0, 0};
solution.merge(list, 3, new int[] {2, 5, 6}, 3);
for (int i = 0; i < 6; i++) {
System.out.println("list[i] = " + list[i]);
}
list = new int[] {2,5,6,7,0,0};
solution.merge(list, 4, new int[] {1, 8}, 2);
for (int i = 0; i < 6; i++) {
System.out.println("list[i] = " + list[i]);
}
}
}