Leetcode 493.翻转对

翻转对

给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对

你需要返回给定数组中的重要翻转对的数量。

示例 1:

输入: [1,3,2,3,1]

输出: 2

示例 2:

输入: [2,4,3,5,1]

输出: 3

注意:

  1. 给定数组的长度不会超过50000。
  2. 输入数组中的所有数字都在32位整数的表示范围内。

 

 

 1 class Solution {
 2     public int reversePairs(int[] nums) {
 3         //给定数组,求符合条件的逆序对
 4         //思路:同剑指offer的类似,但是每次合并需要从头开始合并,同时排序
 5         if(nums==null||nums.length==0) return 0;
 6         return reversePairsCore(nums,0,nums.length-1);
 7     }
 8     public int reversePairsCore(int [] nums,int start,int end){
 9         if(start>=end){
10             return 0;
11         }else{
12             int mid=start+(end-start)/2;
13             int left=reversePairsCore(nums,start,mid);
14             int right=reversePairsCore(nums,mid+1,end);
15 
16             int [] copy=new int[end-start+1];
17             int i=start,t=start;
18             int j=mid+1;
19             int curIndex=0;
20             int count=0;
21 
22             for(;j<=end;j++,curIndex++){
23                 //查找满足条件的
24                 while(i<=mid&&nums[i]<=2*(long)nums[j]){
25                     i++;
26                 }
27                 //排序
28                 while(t<=mid&&nums[t]<nums[j]){
29                     copy[curIndex++]=nums[t++];
30                 }
31 
32                 //将j放入
33                 copy[curIndex]=nums[j];
34                 count+=mid-i+1;
35             }
36 
37             //防止还有后面大的元素
38             while(t<=mid){
39                 copy[curIndex++]=nums[t++];
40             }
41             //将数组赋值给nums
42             System.arraycopy(copy,0,nums,start,end-start+1);
43 
44             return left+count+right;
45 
46         }
47     }
48 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值