-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution88.java
executable file
·35 lines (33 loc) · 1 KB
/
Solution88.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
import java.util.Arrays;
/**
* Created by slade on 2019/8/14.
*/
public class Solution88 {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int nums1_idx = m - 1;
int nums2_idx = n - 1;
for (int i = 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--;
}
} else if (nums2_idx >= 0) {
nums1[i] = nums2[nums2_idx];
nums2_idx--;
}
}
}
public static void main(String[] args) {
Solution88 s = new Solution88();
int[] nums1 = {1, 2, 3, 0, 0, 0};
int m = 3;
int[] nums2 = {2, 5, 6};
int n = 3;
s.merge(nums1, m, nums2, n);
System.out.println(Arrays.toString(nums1));
}
}