面试题 36: 数组中的逆序对
来源:互联网 发布:python删除文件 编辑:程序博客网 时间:2024/06/10 00:12
一. 题目
在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数.
代码请到我的代码库中下载 Point2Offer
二. 代码
package ween_2;/**难度系数:**** * 剑指offer: 数组中的逆序对 * 方法:归并排序思想 * 测试用例:功能测试(123456,135246,654321,52135,31,1) * 特殊输入测试(数组为空) * @author dingding * Date:2017-6-22 7:30 * Declaration: All Rights Reserved! */public class No36 { public static void main(String[] args) { test1(); test2(); test3(); test4(); test5(); test6(); test7(); } //逆序对 private static int inversePairs(int[] arr,int length){ if (arr == null || arr.length<1) { return 0; } int[] copy = new int[length]; //辅助数组 for (int i=0;i<length;i++){ copy[i]=arr[i]; } int count = inversePairsCore(arr,copy,0,length-1); //计算逆序对 return count; } private static int inversePairsCore(int[] arr, int[] copy, int start, int end) { //递归结束条件 if (start == end) { //copy[start] = arr[start]; //分解成单元素后的处理 return 0; } int length = (end - start)/2; int left = inversePairsCore(copy, arr, start, start+length); //会一直执行到完成 (排序,注意参数1和2是反的) int right = inversePairsCore(copy, arr,start+length+1, end); /*合并,排序,并统计逆序对*/ //i初始化为前半段最后一个数字的坐标 int i = start+length; //j初始化为后半段最后一个数字的坐标 int j = end; int indexCopy = end; //辅助数组的指针位置 int count = 0; //可把i,j看出是两个指针 while (i >= start && j>=start+length+1){ if (arr[i]>arr[j]) { copy[indexCopy--]=arr[i--]; count += j-start-length; }else { copy[indexCopy--]=arr[j--]; } } //右半边用尽,取左半边元素 for (;i>=start;i--){ copy[indexCopy--]=arr[i]; } //左半边用尽,取右半边元素 for (;j>=start+length+1;j--){ copy[indexCopy--]=arr[j]; } return left+right+count; } /*==================测试用例==============*/ private static void test(int[] data,int length,int expected){ if(inversePairs(data, length)== expected) System.out.println("Passed."); else System.out.println("Failed."); } private static void test1() { int[] data = {7,5,6,4}; int expected = 5; test(data, data.length, expected); } private static void test2() { int[] data = {6, 5, 4, 3, 2, 1}; int expected = 15; test(data, data.length, expected); } private static void test3() { int[] data = {1, 2, 3, 4, 5, 6}; int expected = 0; test(data, data.length, expected); } private static void test4() { int[] data = {1}; int expected = 0; test(data, data.length, expected); } private static void test5() { int[] data = {1, 2}; int expected = 0; test(data, data.length, expected); } private static void test6() { int[] data = {2, 1}; int expected = 1; test(data, data.length, expected); } private static void test7() { int[] data = {1, 2, 1, 2, 1}; int expected = 3; test(data, data.length, expected); }}
有不妥当之处,麻烦告知:D
阅读全文
0 0
- 面试题36:数组中的逆序对
- 数组中的逆序对(面试题 36)
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题 36: 数组中的逆序对
- 面试题36:数组中的逆序对
- 面试题29:数组中的逆序对
- 面试题40:数组中的逆序对
- 面试题36. 数组中的逆序对
- 剑指offer面试题36:数组中的逆序对
- [剑指offer][面试题36]数组中的逆序对
- 【剑指offer】面试题36:数组中的逆序对
- python 去除字符串两端的引号
- 设计模式六大原则例子(二)-- 单一职责原则(SRP)例子
- GLSL(6)分别使用可编程渲染管线编程和固定渲染管线编程来画点(理解VAO&VBO)
- dpkg: 另外一个进程已经为状态数据库加了锁
- NXP QN902X蓝牙QPPS 流程的理解
- 面试题 36: 数组中的逆序对
- 开源项目material-dialogs使用
- Unsupported major.minor version (jdk版本错误)解决方案 办法
- 守护进程
- QT-QComboBox 调用clear异常
- 修改Apache默认访问目录
- 如何做新品发布会直播(新品推介会直播)基于云直播平台
- linux系统编程之信号(三):信号安装、signal、kill,arise讲解
- 算法之整数交换