From a6b4c62dc590d5ce2fbdc83ae030f3b726979f88 Mon Sep 17 00:00:00 2001 From: algorithmzuo Date: Mon, 17 Jan 2022 19:29:01 +0800 Subject: [PATCH] modify code --- src/class04/Code04_BiggerThanRightTwice.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/class04/Code04_BiggerThanRightTwice.java b/src/class04/Code04_BiggerThanRightTwice.java index 61a6a26..7eb4f7e 100644 --- a/src/class04/Code04_BiggerThanRightTwice.java +++ b/src/class04/Code04_BiggerThanRightTwice.java @@ -1,8 +1,9 @@ package class04; +// 本题测试链接 : https://leetcode.com/problems/reverse-pairs/ public class Code04_BiggerThanRightTwice { - public static int biggerTwice(int[] arr) { + public static int reversePairs(int[] arr) { if (arr == null || arr.length < 2) { return 0; } @@ -19,19 +20,16 @@ public class Code04_BiggerThanRightTwice { } public static int merge(int[] arr, int L, int m, int r) { - // [L....M] [M+1....R] - + // [L....M] [M+1....R] int ans = 0; // 目前囊括进来的数,是从[M+1, windowR) int windowR = m + 1; for (int i = L; i <= m; i++) { - while (windowR <= r && arr[i] > (arr[windowR] * 2)) { + while (windowR <= r && (long) arr[i] > (long) arr[windowR] * 2) { windowR++; } ans += windowR - m - 1; } - - int[] help = new int[r - L + 1]; int i = 0; int p1 = L; @@ -124,7 +122,7 @@ public class Code04_BiggerThanRightTwice { for (int i = 0; i < testTime; i++) { int[] arr1 = generateRandomArray(maxSize, maxValue); int[] arr2 = copyArray(arr1); - if (biggerTwice(arr1) != comparator(arr2)) { + if (reversePairs(arr1) != comparator(arr2)) { System.out.println("Oops!"); printArray(arr1); printArray(arr2);