The class Solution {
Private long sum;
Public int reversePairs (int [] nums) {
sum=0;
Int l=0;
Int r=nums. Length - 1.
Divide (nums, l, r);
Return (int) sum;
}
Public void divide (int [] nums, int l, int r) {
If (l!=r) {
Int mid=(l + r) & gt;> 1;
divide (nums, l, mid);
Divide (nums, mid + 1, r)
Merge (nums, l, mid, r);
}
}
Public void merge (int [] nums, int l, int mids, int r) {
Int [] temp=new int [r - l + 1);
Int I=l;
Int j=mid + 1;
int index=0;
While (i<=mid& & J<=r) {
If (nums [I] & gt; Nums [j]) {
\ [index++]=nums [j++];
//the line is the core, the statistical number of reverse order other code is to merge sort
The sum +=mid - I + 1;
} else {
\ [index++]=nums [i++];
}
}
While (i
}
While (j<=r) {
\ [index++]=nums [j++];
}
The index=0;
For (int k=l; k<=r; K++) {
Nums=\ [k] [index++];
}
}
}
CodePudding user response:
Without considering empty array...CodePudding user response: