From 1c2268e94f8579aede3bcb16454e7a04a6ae0f37 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Wed, 28 Oct 2026 13:45:06 +0800 Subject: [PATCH] =?UTF-8?q?Update=2051.=20=E6=95=B0=E7=BB=84=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E9=80=86=E5=BA=8F=E5=AF=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。 --- notes/51. 数组中的逆序对.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/notes/51. 数组中的逆序对.md b/notes/51. 数组中的逆序对.md index 959857d9..de00a5fe 100644 --- a/notes/51. 数组中的逆序对.md +++ b/notes/51. 数组中的逆序对.md @@ -1,6 +1,6 @@ # 51. 数组中的逆序对 -[NowCoder](https://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/shu-zu-zhong-de-ni-xu-dui-lcof/) ## 题目描述 @@ -9,13 +9,17 @@ ## 解题思路 ```java -private long cnt = 0; -private int[] tmp; // 在这里声明辅助数组,而不是在 merge() 递归函数中声明 +private int cnt = 0; +private int[] tmp; -public int InversePairs(int[] nums) { - tmp = new int[nums.length]; - mergeSort(nums, 0, nums.length - 1); - return (int) (cnt % 1000000007); +public int reversePairs(int[] record) { + if (record == null || record.length < 2) { + return 0; + } + this.cnt = 0; + this.tmp = new int[record.length]; + mergeSort(record, 0, record.length - 1); + return cnt; } private void mergeSort(int[] nums, int l, int h) {