lintode(532)逆序对
来源:互联网 发布:长沙软件培训机构 编辑:程序博客网 时间:2024/06/16 05:13
描述:
在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。给你一个数组,求出这个数组中逆序对的总数。
概括:如果a[i] > a[j] 且 i < j, a[i] 和 a[j] 构成一个逆序对。
样例:
序列 [2, 4, 1, 3, 5]
中,有 3 个逆序对 (2, 1)
, (4, 1)
, (4, 3)
,则返回 3
。
思路:
归并排序,复制一个数组保存排序结果,然后递归解题。一开始嵌套for循环来做,超时了,参考了其他人的方法
public class Solution { /** * @param A an array * @return total of reverse pairs */ //归并排序 public long reversePairs(int[] A) { // Write your code here long result = 0; if(A == null || A.length == 0){ return result; } int[] a = new int[A.length]; for(int i = 0;i<A.length;i++){ a[i] = A[i]; } result = search(A , a , 0 , A.length - 1); return result; } public long search(int[] A , int[] a , int low , int high){ if(high == low){ return 0; } int mid = (low + high)/2; long left = search(A , a , low , mid); long right = search(A , a , mid + 1 , high); int i = mid; int j =high; long result = 0; int copy = high; while(i>=low && j>mid){ if(A[i] > A[j]){ a[copy--] = A[i--]; result += j-mid; }else{ a[copy--] = A[j--]; } } for(;i>=low;i--){ a[copy--] = A[i]; } for(;j>mid;j--){ a[copy--] = A[j]; } for(int p = low;p<=high;p++){ A[p] = a[p]; } return (result+left+right); }}
0 0
- lintode(532)逆序对
- 逆序数 & 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- 逆序对
- log4j使用总结
- MySQL 对一段时间内每天数据统计案例--sql优化
- 前端之常见jquery方法
- 实现3D旋转下拉菜单
- Spring学习(4):Spring数据库事务管理
- lintode(532)逆序对
- 编译项目时出现应该有伪属性名
- 网站获取微信授权登录功能
- 贝叶斯定理
- Redis的三种启动方式
- linux下重启tomcat
- org.apache.hadoop.hbase.TableNotFoundException: SYSTEM.CATALOG exception with phoenix 4.5.2
- NERDTree 快捷键
- Log4j的应用