import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型
     */

    public int mergeSort(int[] array, int start, int end) {
        if (start < end) {
            int mid = (start + end) / 2;
            int left = mergeSort(array, start, mid);
            int right = mergeSort(array, mid + 1, end);
            int count = merge(array, start, mid, end);
            return (left + right + count) % 1000000007;
        }
        return 0;
    }

    public int merge(int[] array, int start, int mid, int end) {
        int[] tempArray = new int[end - start + 1];
        int p1 = start;
        int p2 = mid + 1;
        int p = 0;
        int count = 0;
        while ((p1 <= mid) && (p2 <= end)) {
            if (array[p1] <= array[p2]) {
                tempArray[p++] = array[p1++];
            } else {
                tempArray[p++] = array[p2++];
                count += mid - p1 + 1;
            }
        }
        while (p1 <= mid) {
            tempArray[p++] = array[p1++];
        }
        while (p2 <= end) {
            tempArray[p++] = array[p2++];
        }
        for (int i = 0; i < tempArray.length; i++) {
            array[i + start] = tempArray[i];
        }
        return count % 1000000007;
    }


    public int InversePairs(int[] nums) {
        // write code here
        if (nums == null || nums.length == 0) {
            return 0;
        }
        return mergeSort(nums, 0, nums.length - 1);
    }
}

函数名都没改,直接用的归并,可能比较难理解的就是真正的核心一句

原因大概是这样,这就是为什么逆序对会和归并扯上关系的原因