数组中的逆序对
来源:互联网 发布:网络打鱼游戏版本费 编辑:程序博客网 时间:2024/06/11 02:19
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。
class Solution {public: vector<int> copyData; vector<int> data; int InversePairs(vector<int> newData) { data=newData; if(data.size()==0) return 0; for(int i=0;i<data.size();i++) copyData.push_back(data[i]); int count=InversePairsCore(0,data.size()-1); return count; } int InversePairsCore(int start,int end){ if(start==end){ copyData[start]=data[start]; return 0; } int length=(end-start)/2; int left=InversePairsCore(start,start+length); int right=InversePairsCore(start+length+1,end); int i=start+length; int j=end; int indexCopy=end; int count=0; while(i>=start&&j>=start+length+1){ if(data[i]>data[j]){ copyData[indexCopy--]=data[i--]; count+=j-start-length; }else copyData[indexCopy--]=data[j--]; } for(;i>=start;--i) copyData[indexCopy--]=data[i]; for(;j>=start+length+1;j--) copyData[indexCopy--]=data[j]; for(int i=0;i<data.size();i++) data[i]=copyData[i]; return left+right+count; }};
0 0
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 数组中的逆序对
- 第一个只出现一次的字符
- 证明静态函数在main函数运行前就运行了
- UVA572 Oil Deposits dfs
- 3DMAX 合并镜像物体
- hdu5563解题报告(计算几何)
- 数组中的逆序对
- Android SharedPreferences存储数据的使用方法
- 网页切图的技巧及经验总结
- java解惑--谜题12:ABC
- 【Java基础提高】深入分析IntHashMap(三)
- 【人月神话】读书笔记第7章 为什么巴比伦塔会失败
- 两个链表的第一个公共结点
- shell变量数学运算
- tableView 详细使用方法