-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Offer51.java
53 lines (44 loc) · 1.18 KB
/
Offer51.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
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.yore.offer;
/**
* @author jia bing wen
* @date 2021/7/19 9:11
* @description
*/
public class Offer51 {
int[] nums, tmp;
public int reversePairs(int[] nums) {
this.nums = nums;
tmp = new int[nums.length];
return mergeSort(0, nums.length - 1);
}
private int mergeSort(int l, int r) {
// 终止条件
if (l >= r) {
return 0;
}
// 递归划分
int m = (l + r) / 2;
int res = mergeSort(l, m) + mergeSort(m + 1, r);
// 合并阶段
int i = l, j = m + 1;
for (int k = l; k <= r; k++) {
tmp[k] = nums[k];
}
for (int k = l; k <= r; k++) {
if (i == m + 1) {
nums[k] = tmp[j++];
} else if (j == r + 1 || tmp[i] <= tmp[j]) {
nums[k] = tmp[i++];
} else {
nums[k] = tmp[j++];
res += m - i + 1; // 统计逆序对
}
}
return res;
}
public static void main(String[] args) {
Offer51 o = new Offer51();
int count = o.reversePairs(new int[]{7,5,6,4});
System.out.println(count);
}
}