数组中的逆序对

class Solution {
public:
    int InversePairs(vector<int> data) {
        int len = data.size();
        if (len <= 1)
        {
            return 0;
        }
        map<int,int> mm;
        int tem;
        for (int k = len -1;k > 0; --k)
        {
            bool done = 1;
            for(int i = 0 ;i < k ; ++i)
            {
                if(data[i] > data[i+1])
                {
                    done = 0;
                    if(mm.count(data[i])==0)
                    {
                        mm[data[i]] = 1;
                    }
                    else
                    {
                        ++mm[data[i]];
                    }
                    tem = data[i];
                    data[i] = data[i+1];
                    data[i+1] = tem;
                }
            }
            if(done)
            {
                break;
            }
        }

        int sum = 0;
        for (map<int,int>::iterator it = mm.begin();it != mm.end() ; ++ it)
        {
            sum += it->second;
        }
        return sum;
    }
};

 

posted @ 2016-08-30 19:40  随风9  阅读(185)  评论(0编辑  收藏  举报