本文共 2248 字,大约阅读时间需要 7 分钟。
import netscape.security.UserTarget;/** * 面试题51:数组中的逆序对 * 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。 * 输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007 * 输入:[1,2,3,4,5,6,7,0] * 输出:7 * @author * @create 2021-04-07 23:14 */public class Solution51 { static int count; public static void main(String[] args) { int[] arr = { 1,2,3,4,5,6,7,0}; int inverseNum = inversePairs(arr); System.out.println(inverseNum); } public static int inversePairs(int [] array) { int[] temp = new int[array.length]; mergeSort(array,0,array.length-1,temp); return count; } /** * 以下为使用归并排序统计逆序对数,仅在归并排序时加上count += mid - l + 1;即可。原因如下: * 在归并排序过程中统计逆序对数,当左右有序数组长度都为1时,如果左边大于右边逆序对加1,比如[5],[4] * 在合并过程中,比如[4,5],[2,7],由于4>2,那么4与2构成一组逆序对,而[4,5]是排好序的,那么5与2也构成逆序对, * 因此此时的逆序对数为mid-l+1 * 已经排序的数组内部就不用再统计逆序对数 * 时间复杂度O(nlogn),空间复杂度O(n),以空间换时间 * @param arr * @param left * @param right * @param temp */ public static void mergeSort(int[] arr, int left, int right, int[] temp){ if (left < right){ int mid = (right + left) / 2; mergeSort(arr, left, mid, temp);//向左递归分解 mergeSort(arr, mid + 1, right, temp);//向右递归分解 merge(arr, left, right, mid, temp);//合并 } } public static void merge(int[] arr, int left, int right, int mid, int[] temp){ int l = left;//左指针指向左边有序数组的开始 int r = mid+1;//右指针指向右边有序数组的开始 int tempIndex = 0;//辅助数组的索引 //①先把左右两边(有序)的数据按照规则填充到temp数组 //直到左右两边的有序序列,有一边处理完毕为止 while (l <= mid && r <= right){ if (arr[l] <= arr[r]){ temp[tempIndex++] = arr[l++]; }else { temp[tempIndex++] = arr[r++]; //此时统计逆序对数 count += mid - l + 1;//arr[l] > arr[r]说明arr[l...mid]都大于arr[r] } } //②//把有剩余数据的一边的数据依次全部填充到temp while (l <= mid){ temp[tempIndex++] = arr[l++]; } while (r <= right){ temp[tempIndex++] = arr[r++]; } //③将temp数组的元素拷贝到arr tempIndex = 0; int leftTemp = left; while (leftTemp <= right){ arr[leftTemp++] = temp[tempIndex++]; } }}
转载地址:http://eufdz.baihongyu.com/