剑指offer之找出数组中的逆序对
来源:互联网 发布:内容营销的出路 知乎 编辑:程序博客网 时间:2024/06/02 04:34
在数组中的俩个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组的逆序对的总数。
看到这个问题时,我先想到的方法是遍历整个数组,扫描到一个数字,逐个比较它后面的数字大小。如果后面的数字比它小,则这两个数字就组成一个逆序对,这种方法的是假复杂度是O(N*N),显然,我们必须找到更加高效的算法。
想想其实可以使用排序来解决这个问题,每次数组里的数据发生一次交换,就说明有一对逆序对,我们可以选择的排序有快速排序和堆排序,这两种排序的时间效率都是比较高的都是O(lgN)。
但是,还有一中排序和找逆序 对的思想一致,那就是归并排序。归并排序就是把一个数组分成前半段和后半段,然后又拆分前半段为两段,后半段为两段。。。这样拆分下去,最后的子数组长度会为1,开始归并,每一次归并两个数组的时候,如果左数组比右数组大,那么着就是一个逆序。记录所有左数组比右数组大的情况,就是全部的逆序数目。
<pre name="code" class="cpp">int InversePairsCore(int* data, int* copy, int start, int end) {if (start == end) {copy[start] = data[start];return 0;}int length = (end-start)/2;int left = InversePairsCore(copy, data, start, start+length);int right = InversePairsCore(copy, data, 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]) {copy[indexCopy--] = data[i--];count += j-start-length;} else {copy[indexCopy--] = data[j--];}}for (; i>=start; --i)copy[indexCopy--] = data[i];for (; j>=start+length+1; --j)<pre name="code" class="cpp">copy[indexCopy--] = data[j];return left+right+count;}int InversePairs (int* data, int length){if (data == NULL || length < 0)return 0;int *copy = new int[length];for (int i=0; i<length;++i) copy[i]=data[i]; int count=InversePairCore(data,copy,0,length); delete copy; return count;}
0 0
- 剑指offer之找出数组中的逆序对
- 剑指Offer之 - 数组中的逆序对
- 剑指offer之数组中的逆序对
- 【剑指offer】之数组中的逆序对
- 剑指offer系列之三十四:数组中的逆序对
- 剑指offer系列之34:数组中的逆序对
- 剑指offer之面试题36数组中的逆序对
- 剑指Offer之面试题36:数组中的逆序对
- 【剑指offer】数组中的逆序对
- 剑指offer 36 数组中的逆序对
- 剑指offer--数组中的逆序对
- 剑指offer 36 - 数组中的逆序对
- 《剑指offer》数组中的逆序对
- 【剑指offer】数组中的逆序对
- 剑指offer—数组中的逆序对
- 剑指offer:数组中的逆序对
- [剑指offer]数组中的逆序对
- 剑指offer:数组中的逆序对
- hadoop 的编译
- bzoj 1690(01规划:最优比率环)
- iOS开发之Architectures设置
- iOS 如何关联xib和控制器
- python编码问题 -- UnicodeEncodeError: 'ascii' codec can't encode characters in position
- 剑指offer之找出数组中的逆序对
- session入库
- mongodb备份恢复
- 下拉选择菜单封装
- Quartz教程三:Job与JobDetail介绍
- 增加机房页面需求之问题汇总
- 遍历js中对象的属性和值
- css position属性
- Happy Number