zoukankan      html  css  js  c++  java
  • 数组中的逆序对

    class Solution {
    public:
        int InversePairs(vector<int> data) {
            if(data.empty())
                return 0;
            int n=data.size();
            vector<int> copy(n);
            return Inverse(data,copy,0,n-1);
        }
        int Inverse(vector<int> &data,vector<int> &copy,int start,int end)
        {
            if(start==end)
            {
                return 0;
            }
            int mid=(start+end)>>1;
            int left=Inverse(data,copy,start,mid);
            int right=Inverse(data,copy,mid+1,end);
            int count=0;
            int ll=mid;
            int rr=end;
            int k=end;
            while(ll>=start&&rr>mid)
            {
                if(data[ll]>data[rr])
                {
                    count+=rr-mid;
                    copy[k--]=data[ll--];
                }
                else
                {
                    copy[k--]=data[rr--];
                }
            }
            while(ll>=start)
                copy[k--]=data[ll--];
            while(rr>mid)
                copy[k--]=data[rr--];
            for(int i=start;i<=end;i++)
                data[i]=copy[i];
            return left+right+count;
        }
    };
  • 相关阅读:
    golang 类型断言的学习
    如何查询每个用户的第二条记录
    PHP Slim 框架初体验之无法访问控制器
    jquery循环遍历radio单选按钮,并设置选中状态
    CI框架中自定义view文件夹位置
    PHP代码实现MySQL读写分离
    mysql实现主从复制
    wildflyのデプロイ後の保存位置
    postgresql function
    shell backup
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4658856.html
Copyright © 2011-2022 走看看