数组中的逆序对
来源:互联网 发布:韩剧软件 编辑:程序博客网 时间:2024/06/07 16:16
题目:
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
思路:
待补充。。。
代码:
public class Solution { public int InversePairs(int [] array) { if(array == null || array.length < 0){ return 0; } int[] copy = new int[array.length]; for(int i = 0;i < array.length;++i){ copy[i] = array[i]; } int count = InversePairsCore(array,copy,0,array.length - 1); return count; } public int InversePairsCore(int[] array,int[] copy,int start,int end){ if(start == end){ copy[start] = array[start]; return 0; } int length = (end - start)/2; int left = InversePairsCore(copy,array,start,start + length); int right = InversePairsCore(copy,array,start + length + 1,end); int i = start + length; int j = end; int indexCopy = end; int count = 0; while(i >= start && j > start + length){ if(array[i] > array[j]){ copy[indexCopy--] = array[i--]; count += j - start - length; if(count > 1000000007){ count %= 1000000007; } }else{ copy[indexCopy--] = array[j--]; } } for(;i >= start;--i){ copy[indexCopy--] = array[i]; } for(;j >= start + length + 1;--j){ copy[indexCopy--] = array[j]; } return ((left + right) % 1000000007 + count % 1000000007); }}
阅读全文
0 0
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- Java set集合 交集,并集,差集
- python之selenium安装及使用方法简介
- 信息熵 交叉熵 相对熵 条件熵
- hdu5608 function
- mysql监控工具断网部署Percona Monitoring and Management ,Perocona的官方监控工具Docker安装教程
- 数组中的逆序对
- 狗逃离迷宫
- jmap命令打印堆栈信息文件
- Java 7之基础
- JS DOM(文档对象模型)与BOM(浏览器对象模型)
- Visual Studio 匹配花括号的背景颜色
- mybatis 批量插入数据(转载留存)
- Hololens开发初级 系列 1-安装开发环境
- 文章标题