栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

LeeCode题目——数组中的逆序对

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeeCode题目——数组中的逆序对

文章目录
  • 数组中的逆序对
    • 解题思路
    • python代码实现
    • java代码实现

数组中的逆序对

题目描述:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对总数P,并将其P对1000000007取模的结果输出。
要求:空间复杂度为O(n),时间复杂度为O(nlogn)。
示例
输入 :[1,2,3,4,5,6,7,0]
输出:7

解题思路

利用暴力方法的时间复杂度为O(n^2),显然不满足要求。
假设存在两个区间[4,3]和[1,2],那么逆序对为(4,1),(4,2),(3,1),(3,2),当两个区间变的有序时,逆序对的结果一样,这就说明区间有序和无序结果是一样的。
但是如果区间有序,当其中一个元素i与元素j构成逆序对时,元素i后面的元素都可以与元素j构成逆序对。

  • 划分:将数组划分为两个区间,并进行递归划分,直到区间的大小为1;
  • 排序:使用归并排序处理两个区间,在比较元素大小时,统计逆序对;
  • 合并:合并两个区间,并累加逆序对。
python代码实现
class Solution:
    count = 0
    #两个区间合并
    def merge(self, left, right):
        res = []
        m = len(left)
        n = len(right)
        i = 0
        j = 0
        while i 
java代码实现 
class Solution {
    public static int count = 0;
    public void merge(int[] array,int low,int mid,int high){
        
        int[] temp = new int[high-low+1];
        int i = low;
        int j = mid+1;
        int k = 0;
        while(i<=mid && j<=high){
            if(array[i]<=array[j]){
                temp[k++] = array[i];
                i++;
            } else{
                temp[k++] = array[j];
                j++;
                count += (mid+1-i);
            }
        }
        while(i<=mid) temp[k++] = array[i++];
        while(j<=high) temp[k++] = array[j++];
        for(int l=0;l
            array[low+l] = temp[l];
        }
    }
    public void mergeSort(int[] array,int low,int high){
        if(low
            int mid = (low+high)/2;
            this.mergeSort(array,low,mid);
            this.mergeSort(array,mid+1,high);
            this.merge(array,low,mid,high);
        }
    }
    public int reversePairs(int[] nums) {
        if(nums.length<2) return 0;
        this.mergeSort(nums,0,nums.length-1);
        return count;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号