java实现数组中的逆序对
作者:雨幕下的稻田 时间:2021-12-07 16:36:24
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对,例如在数组{7,5,6,4}中,一共存在5对逆序对,分别是{7,6},{7,5},{7,4},{6,4},{5,4}。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。,即输出P%1000000007。
代码
解法一
暴力简单低效,不会改变原数组
public static int inversePairs(int[] array) {
if (array == null || array.length < 2) {
return 0;
}
int count = 0;
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
count++;
}
}
}
return count % 1000000007;
}
解法二
利用数组的归并排序,高效,但是会改变原数组
public static int inversePairs2(int[] array) {
if (array == null || array.length < 2) {
return 0;
}
int count = mergeSort(array, 0, array.length - 1);
return count % 1000000007;
}
private static int mergeSort(int[] array, int start, int end) {
if (start >= end) {
return 0;
}
// 找到数组的中点,分割为两个子数组,递归求解
int middle = (start + end) / 2;
int left = mergeSort(array, start, middle);
int right = mergeSort(array, middle + 1, end);
// 存储归并后的数组
int[] copy = new int[array.length];
System.arraycopy(array, start, copy, start, end - start + 1);
// 从两个子数组的尾部开始遍历
int i = middle;
int j = end;
int copyIndex = end;
// 记录逆序对的数量
int count = 0;
while (i >= start && j >= middle + 1) {
// 数组是升序的
// 如果左边数组比右边数组大,则将大的放入存储数组中
// 并且累加逆序对,应为是有序的,所以左边数组的第i个元素比第j个及其之前的数都大
if (array[i] > array[j]) {
copy[copyIndex--] = array[i--];
count += (j - middle);
} else {
copy[copyIndex--] = array[j--];
}
}
// 将子数组剩余的部分一次写入归并后的存储数组
while (i >= start) {
copy[copyIndex--] = array[i--];
}
while (j >= middle + 1) {
copy[copyIndex--] = array[j--];
}
// 将本次两个子数组的合并写入原数组中
for (int k = start; k <= end ; k++) {
array[k] = copy[k];
}
return left + right + count;
}
来源:https://blog.csdn.net/zl18310999566/article/details/80228859
标签:java,数组,逆序对
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring @schedule注解如何动态配置时间间隔
2023-07-29 03:51:14
Android编程之ListView和EditText发布帖子隐藏软键盘功能详解
2023-11-06 23:09:55
C#实现对文件进行加密解密的方法
2023-05-28 14:02:44
Spring MVC 处理一个请求的流程
2021-12-19 18:39:39
![](https://img.aspxhome.com/file/2023/1/77341_0s.png)
Java控制台实现猜拳游戏小游戏
2022-04-20 09:52:55
![](https://img.aspxhome.com/file/2023/6/80206_0s.jpg)
Java如何跳出当前多重循环你知道吗
2022-12-17 02:22:17
Springboot 全局时间格式化操作
2022-08-17 03:23:51
![](https://img.aspxhome.com/file/2023/2/94332_0s.jpg)
使用java实现猜拳小游戏
2023-11-25 17:30:16
![](https://img.aspxhome.com/file/2023/4/79224_0s.jpg)
Android实现读取NFC卡卡号示例
2021-08-06 21:08:39
C#使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化
2023-01-16 02:11:54
![](https://img.aspxhome.com/file/2023/5/88425_0s.png)
java开发BeanUtils类解决实体对象间赋值
2022-08-25 06:31:25
![](https://img.aspxhome.com/file/2023/0/68150_0s.png)
解析maven的用法和几个常用的命令(推荐)
2022-04-16 23:31:15
![](https://img.aspxhome.com/file/2023/4/87174_0s.jpg)
基于序列化存取实现java对象深度克隆的方法详解
2021-08-31 07:45:26
![](https://img.aspxhome.com/file/2023/4/91074_0s.gif)
SpringBoot 整合mybatis+mybatis-plus的详细步骤
2022-09-29 21:06:10
![](https://img.aspxhome.com/file/2023/5/72915_0s.png)
优化spring boot应用后6s内启动内存减半
2021-09-13 02:47:59
![](https://img.aspxhome.com/file/2023/3/69903_0s.png)
C#手工双缓冲技术用法实例分析
2023-05-09 07:13:44
浅谈c++性能测试工具之计算时间复杂度
2023-07-09 12:40:05
![](https://img.aspxhome.com/file/2023/4/79524_0s.jpg)
Java NIO Path接口和Files类配合操作文件的实例
2023-10-20 09:29:01
Unity shader实现百叶窗特效
2023-06-05 00:05:49
![](https://img.aspxhome.com/file/2023/0/104310_0s.png)
如何使用lamda表达式对list进行求和
2022-08-24 09:20:09