Home > Back-end >  Help (Java) : refers to offer an array of reverse to LeetCode sword, how should I code change don�
Help (Java) : refers to offer an array of reverse to LeetCode sword, how should I code change don�

Time:11-30

My code is allowed, but submit will stack overflow problem, for help - & gt; The red code will stack overflow problems
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\ [index++]=nums [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:

refer to 1st floor met the goddess response:
didn't consider the condition of the empty array...

Thanks for elder brother
I'm head confused ^ ^
  • Related