数组中的逆序对
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。
示例
输入: [7,5,6,4]
输出: 5
代码
/**
* @param {number[]} nums
* @return {number}
*/
var reversePairs = function(nums) {
// 归并排序
let sum = 0;
mergeSort(nums);
return sum;
function mergeSort (nums) {
if(nums.length < 2) return nums;
const mid = parseInt(nums.length / 2);
let left = nums.slice(0,mid);
let right = nums.slice(mid);
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
let res = [];
let leftLen = left.length;
let rightLen = right.length;
let len = leftLen + rightLen;
for(let index = 0, i = 0, j = 0; index < len; index ++) {
if(i >= leftLen) res[index] = right[j ++];
else if (j >= rightLen) res[index] = left[i ++];
else if (left[i] <= right[j]) res[index] = left[i ++];
else {
res[index] = right[j ++];
sum += leftLen - i;//在归并排序中唯一加的一行代码
}
}
return res;
}
};
FEATURED TAGS
前端开发
H5
JavaScript
设计模式
browser
jQuery
源码分析
生活
leetcode
Array
Stack
Queue
Linked List
剑指offer
Binary Search Tree
Binary Tree
Breadth-First Search
Depth-First Search
String
Set
Binary Search
Sliding Window
Backtracking
Dynamic Programming
Two Pointers
Union Find
Sort
Bit Operation
Recursion
Map
Graph
Search
Hash
LinkedList
复盘
QuickSort
Trie
Design
MinHeap
Traverse
Min Heap
Node.js
BackEnd
SQL
MySQL
Design Patterns
Network
计算机网络
Python
SVG