- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution88.java
More file actions
Latest commit
executable file
·35 lines (33 loc) · 1 KB
/
Copy pathSolution88.java
File metadata and controls
executable file
·35 lines (33 loc) · 1 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
importjava.util.Arrays;
/**
* Created by slade on 2019/8/14.
*/
publicclassSolution88 {
publicvoidmerge(int[] nums1, intm, int[] nums2, intn) {
intnums1_idx = m - 1;
intnums2_idx = n - 1;
for (inti = m + n - 1; i > -1; i--) {
if (nums1_idx >= 0 && nums2_idx >= 0) {
if (nums1[nums1_idx] > nums2[nums2_idx]) {
nums1[i] = nums1[nums1_idx];
nums1_idx--;
} else {
nums1[i] = nums2[nums2_idx];
nums2_idx--;
}
} elseif (nums2_idx >= 0) {
nums1[i] = nums2[nums2_idx];
nums2_idx--;
}
}
}
publicstaticvoidmain(String[] args) {
Solution88s = newSolution88();
int[] nums1 = {1, 2, 3, 0, 0, 0};
intm = 3;
int[] nums2 = {2, 5, 6};
intn = 3;
s.merge(nums1, m, nums2, n);
System.out.println(Arrays.toString(nums1));
}
}