forked from algorithm020/algorithm020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortedArray.java
More file actions
61 lines (47 loc) · 1.66 KB
/
MergeSortedArray.java
File metadata and controls
61 lines (47 loc) · 1.66 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Arrays;
class Solution {
//方式一、将num2的数据copy到num1中,然后排序
public void merge1(int[] nums1, int m, int[] nums2, int n) {
System.arraycopy(nums2, 0, nums1, m, n);
Arrays.sort(nums1);
}
//方式二、从后向前比较,将较大的数据从后往前放。
public void merge2(int[] nums1, int m, int[] nums2, int n) {
int index = m + n - 1;
int r1 = m - 1;
int r2 = n - 1;
// while (r1 >= 0 && r2 >= 0) {
// while (r1 >= 0 && nums1[r1] > nums2[r2]) {
// nums1[index--] = nums1[r1--];
// }
// while (r1 >= 0 && r2 >= 0 && nums1[r1] <= nums2[r2]) {
// nums1[index--] = nums2[r2--];
// }
// }
//优化后的代码.
while (r1 >= 0 && r2 >= 0) {
nums1[index--] = nums1[r1] > nums2[r2] ? nums1[r1--] : nums2[r2--];
}
//此处可以省略,因此本身就在num1数组上操作.
// while (r1 >= 0) {
// nums1[index--] = nums1[r1--];
// }
while (r2 >= 0) {
nums1[index--] = nums2[r2--];
}
}
//最优的代码.
public void merge(int[] nums1, int m, int[] nums2, int n) {
int index = m + n - 1;
int r1 = m - 1;
int r2 = n - 1;
while (r1 >= 0 && r2 >= 0) {
nums1[index--] = nums1[r1] > nums2[r2] ? nums1[r1--] : nums2[r2--];
}
while (r2 >= 0) {
nums1[index--] = nums2[r2--];
}
}
}
//方式一、将num2的数据copy到num1中,然后排序
//方式二、从后向前比较,将较大的数据从后往前放。