zoukankan      html  css  js  c++  java
  • 剑指offer 面试51题

    面试51题:

    题目:数组中的逆序对

    题目描述

    在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007

    输入描述:

    题目保证输入的数组中没有的相同的数字

    数据范围:

    对于%50的数据,size<=10^4

    对于%75的数据,size<=10^5

    对于%100的数据,size<=2*10^5

    示例1

    输入

    复制
    1,2,3,4,5,6,7,0

    输出

    复制
    7

    解题代码:

    # -*- coding:utf-8 -*-
    class Solution:
        #方法一:巧妙的方法,来源牛客网
        def InversePairs(self, data):
            if len(data) <= 0:
                return 0
            count = 0
            copy = []
            for i in range(len(data)):
                copy.append(data[i])
            copy.sort()
            i = 0
            while len(copy) > i:
                count += data.index(copy[i])
                data.remove(copy[i])
                i += 1
            return count%1000000007
        
        #暴力法
        def InversePairs2(self, data):
            if len(data)<=1:
                return 0
            count=0
            length=len(data)
            for i in range(length-1):
                for j in range(i+1,length):
                    if data[i]>data[j]:
                        count+=1
            return count % 1000000007
        
        #归并排序法
        def InversePairs3(self, data):
            if len(data)<=0:
                return 0
            length=len(data)
            copy=[0]*length
            for i in range(length):
                copy[i]=data[i]
            #copy数组为原数组data的复制,在后面充当辅助数组
            count=self.Core(data,copy,0,length-1)
            return count % 1000000007
        
        def Core(self,data,copy,start,end):
            if start==end:
                copy[start]=data[start]
                return 0
    
            length=(end-start)//2 #length为划分后子数组的长度
    
            left=self.Core(copy,data,start,start+length)
            right=self.Core(copy,data,start+length+1,end)
    
            #初始化i为前半段最后一个数字的下标
            i=start+length
            #初始化j为后半段最后一个数字的下标
            j=end
    
            #indexCopy为辅助数组的指针,初始化其指向最后一位
            indexCopy=end
            #准备开始计数
            count=0
            #对两个数组进行对比取值的操作:
            while i>=start and j>=start+length+1:
                if data[i]>data[j]:
                    copy[indexCopy]=data[i]
                    indexCopy-=1
                    i-=1
                    count += j-start-length
                else:
                    copy[indexCopy]=data[j]
                    indexCopy-=1
                    j-=1
            
            #剩下一个数组未取完的操作:
            while i>=start:
                copy[indexCopy]=data[i]
                indexCopy-=1
                i-=1
            while j>=start+length+1:
                copy[indexCopy]=data[j]
                indexCopy-=1
                j-=1
            
            return count+left+right
  • 相关阅读:
    Servlet Filter
    Analysis of Web.xml in Hello1 project
    Introduction of Annotation
    Analysis of container and Injection in Java, their history and future.
    Maven + Glassfish 实现hello
    java Building tool
    HTTP RFC(自学心得)
    URI和URL
    Git 版本控制
    Oracle RAC 全局等待事件 gc current block busy 和 gc cr multi block request 说明--转载(http://blog.csdn.net/tianlesoftware/article/details/7777511)
  • 原文地址:https://www.cnblogs.com/yanmk/p/9232547.html
Copyright © 2011-2022 走看看