• 数组中的逆序对


    题目来源:
     https://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

    《剑指offer》

    自我感觉难度/真实难度:

    easy 

    题意:
     
    分析:
     
    自己的代码:
    代码效率/结果:
     
    优秀代码:
    //数组中的逆序对
        public static int InversePairs(int[] array){
            if(array==null||array.length<=1)
                return 0;
            int[] copy = new int[array.length];
            for(int i=0;i<array.length;i++){
                copy[i] = array[i];
            }
            return mergeCount(array, copy, 0, array.length-1);
        }
        
        public static int mergeCount(int[] array, int[] copy, int start, int end){
            if(start==end){
                copy[start] = array[start];
                return 0;
            }
            int mid = (start+end)>>1;
            int leftCount = mergeCount(copy, array, start, mid);
            int rightCount = mergeCount(copy, array, mid+1, end);
            
            int i = mid;//i初始化为前半段最后一个数字的下标
            int j = end;//j初始化为后半段最后一个数字的下标
            int index = end;//辅助数组复制的数组的最后一个数字的下标
            int count = 0;//计数--逆序对的数目
            while(i>=start&&j>=mid+1){
                if(array[i]>array[j]){
                    copy[index--] = array[i--];
                    count += j-mid;
                }else{
                    copy[index--] = array[j--];
                }
            }
            for(;i>=start;i--){
                copy[index--] = array[i];
            }
            for(;j>=mid+1;j--){
                copy[index--] = array[j];
            }
            return leftCount+rightCount+count;
        }
    代码效率/结果:
     
    自己优化后的代码:
     
    class Solution:
        def InversePairs(self, data):
            # write code here
            global count
            count=0
            def rev(data):
                global count
                length=len(data)
                if length<=1:
                    return data
                mid=int(length/2)
                left_data=rev(data[:mid])
                right_data=rev(data[mid:])
                l=0
                r=0
                res=[]
                while l<len(left_data) and r<len(right_data):
                    if left_data[l]<right_data[r]:
                        res.append(left_data[l])
                        l+=1
                    else:
                        res.append(right_data[r])
                        r+=1
                        count+=len(left_data)-l
                res+=left_data[l:]
                res+=right_data[r:]
                return res 
            rev(data)
            return count
    a=Solution()
    print(a.InversePairs([1,2,3,4,5,0]))
    反思改进策略:

     1.不会用归并排序,没有想到怎么样最快速实现这个想法

    2.暴利的循环解题肯定是没有好处的,要注意思考和现有的什么算法有异曲同工的地方

    自己手写了一边以后:

    1,while 循环的条件一定要注意是什么条件,很容易出错

    2,递归出口和最后返回的类型要一致

    3,全局变量在使用之前就要声明,声明全局变量使用关键字 global,然后在使用该全局变量之前,需要再次声明

    特别注意,如果是类 class Solution,全局变量应该写在类的最开始,构造函数之前

    4,还是没有一遍就写对,改错了几次,参考了代码,希望坚持白板书写,提高编程能力

    写题时间时长:

    2h

  • 相关阅读:
    [破解] nasca drm file -ver1.00
    [KEY] DK NOTES
    [Java] Tomcat环境变量设置
    [Java] Java record
    [SQL] SQL Server 触发器
    [GitHub] GitHub Repositories
    [GitHub] GitHub使用教程for Eclipse
    [SharePoint] SharePoint 错误集 3
    [SharePoint] SharePoint 错误集 2
    [SharePoint] SharePoint 错误集 1
  • 原文地址:https://www.cnblogs.com/captain-dl/p/10555556.html
Copyright © 2020-2023  润新知