zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    svn出现黄色感叹号怎么办
    数据库设计三大范式
    windows server2008R2 64位 配置 mysql-8.0.15-winx64
    sqlquerystress
    锁表操作
    微软专用消息队列msmq的简单使用
    数据库上移和下移
    mvc全局时间输出格式化处理
    webapi jsonp处理
    泛型处理ToEntity
  • 原文地址:https://www.cnblogs.com/kexinxin/p/10372505.html
Copyright © 2011-2022 走看看