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;
        }
    };
  • 相关阅读:
    266. Palindrome Permutation
    265. Paint House II
    264. Ugly Number II
    263. Ugly Number
    261. Graph Valid Tree
    260. Single Number III
    259. 3Sum Smaller
    pthon 批量压缩当前目录,子目录下图片
    不小心打开了N多的压缩文件窗口,一句命令就搞定!
    # Writing your-first Django-app-part 4-simple-form
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4658856.html
Copyright © 2011-2022 走看看