面试题36:数组中的逆序对
来源:互联网 发布:关于刑侦破案的网络剧 编辑:程序博客网 时间:2024/06/10 02:39
题目:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组的逆序对的总数。
我的思路:实在是没想到什么好的办法,最古老的办法,从第一个元素开始逐一和后面的元素比较大小,如果符合逆序对的要求则count+1.时间复杂度为O(n^2),估计拿不到offer。
正确的答案是采用归并排序的思想,将这个数组分割成小数组,先统计出数组内部的逆序对的个数,然后再统计出两个相邻子数组之间的逆序对的数目。
先复习一下归并排序算法:
归并排序是利用递归和分而治之的技术将数据序列划分成为越来越小的半子表,再对半子表排序,最后再用递归步骤将排好序的半子表合并成为越来越大的有序序列,归并排序包括两个步骤,分别为:
1)划分子表
2)合并半子表
public void MergerSort(int[] v, int first, int last) { if (first + 1 < last) { int mid = (first + last) / 2; MergerSort(v, first, mid); MergerSort(v, mid, last); Merger(v, first, mid, last); } }
public void Merger(int[] v, int first, int mid, int last) { Queue<int> tempV = new Queue<int>(); int indexA, indexB; //设置indexA,并扫描subArray1 [first,mid] //设置indexB,并扫描subArray2 [mid,last] indexA = first; indexB = mid; //在没有比较完两个子标的情况下,比较 v[indexA]和v[indexB] //将其中小的放到临时变量tempV中 while (indexA < mid && indexB < last) { if (v[indexA] < v[indexB]) { tempV.Enqueue(v[indexA]); indexA++; } else { tempV.Enqueue(v[indexB]); indexB++; } } //复制没有比较完子表中的元素 while (indexA < mid) { tempV.Enqueue(v[indexA]); indexA++; } while (indexB < last) { tempV.Enqueue(v[indexB]); indexB++; } int index = 0; while (tempV.Count > 0) { v[first+index] = tempV.Dequeue(); index++; } }
将查找逆序对的思路套进归并排序算法的代码中可以得出:
package a;public class MergeSort {static int count = 0;// 将有二个有序数列a[first...mid]和a[mid...last]合并。static void mergearray(int a[], int first, int mid, int last, int temp[]) {int i = first, j = mid + 1;int m = mid, n = last;int k = 0;while (i <= m && j <= n) {if (a[i] > a[j]) {// 左数组比右数组大temp[k++] = a[j++];// 因为如果a[i]此时比右数组的当前元素a[j]大,// 那么左数组中a[i]后面的元素就都比a[j]大// 【因为数组此时是有序数组】count += mid - i + 1;} else {temp[k++] = a[i++];}}while (i <= m) {temp[k++] = a[i++];}while (j <= n) {temp[k++] = a[j++];}for (i = 0; i < k; i++)a[first + i] = temp[i];}static void mergesort(int a[], int first, int last, int temp[]) {if (first < last) {int mid = (first + last) / 2;mergesort(a, first, mid, temp); // 左边有序mergesort(a, mid + 1, last, temp); // 右边有序mergearray(a, first, mid, last, temp); // 再将二个有序数列合并}}static void mergeSort(int a[]) {int[] p = new int[a.length];mergesort(a, 0, a.length - 1, p);}public static void main(String ss[]) {// int data[] = { 8, 7, 5, 6, 4 };int data[] = { 4, 3, 2, 1 };mergeSort(data);for (int i = 0; i < data.length; i++) {System.out.print(data[i] + "\t");}System.out.println();System.out.println(count);}}
就是在归并的时候,如果出现逆序对就将count+1其余的和归并排序没什么区别。
0 0
- 面试题36:数组中的逆序对
- 数组中的逆序对(面试题 36)
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题 36: 数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题29:数组中的逆序对
- 面试题40:数组中的逆序对
- 面试题36. 数组中的逆序对
- 剑指offer面试题36:数组中的逆序对
- [剑指offer][面试题36]数组中的逆序对
- 【剑指offer】面试题36:数组中的逆序对
- mybatis 返回主键
- 反射与泛型
- Android Runnable 运行在那个线程
- 线程的分离状态
- java中byte, int的转换
- 面试题36:数组中的逆序对
- 使用silverlight后,页面显示不全
- @RequestMapping 用法详解之地址映射
- android添加library遇到的问题
- Lua教程:Lua调用C/C++函数(4)
- iOS开发路线简述
- 文件和数据流
- CmakeManual-Qt5
- 80端口或8080端口被占用的解决办法 .