-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution088.java
40 lines (35 loc) · 1.06 KB
/
Solution088.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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 合并两个有序数组
* @date: 2018/08/17
*/
public class Solution088 {
public static void main(String[] args) {
Solution088 test = new Solution088();
int[] a = new int[]{1, 2, 3, 0, 0, 0};
int[] b = new int[]{2, 5, 6};
test.merge(a, 3, b, 3);
for (int t : a) {
System.out.print(t);
System.out.print(" ");
}
System.out.println();
}
public void merge(int[] nums1, int m, int[] nums2, int n) {
int index1 = m - 1;
int index2 = n - 1;
int mergeIndex = m + n - 1;
while (0 <= index1 || 0 <= index2) {
if (0 > index1) {
nums1[mergeIndex--] = nums2[index2--];
} else if (0 > index2) {
nums1[mergeIndex--] = nums1[index1--];
} else if (nums1[index1] <= nums2[index2]) {
nums1[mergeIndex--] = nums2[index2--];
} else {
nums1[mergeIndex--] = nums1[index1--];
}
}
}
}