<pre name="code" class="java">package test;
public class InversePairs {
public int inversePairs(int[] data){
if(data == null || data.length<0)
return 0;
int[] copy = new int[data.length];
for(int i=0; i<data.length; ++i)
copy[i] = data[i];
int count = inversePairsCore(data, copy,0, data.length-1);
copy = null;
return count;
}
/**
* 归并算法是从前往后插入,这个算法是从后往前插入
*/
public int inversePairsCore(int[] data, int[] copy,int start, int end){
if(start == end){
copy[start] = data[start];
return 0;
}
int length = (end-start)/2;
int left = inversePairsCore(copy, data, start, start + length);
int right = inversePairsCore(copy, data, start+length+1, end);
int i = start + length;
int j = end;
int indexCopy = end;
int count = 0;
while(i >= start && j>=start+length+1){
if(data[i]>data[j]){
copy[indexCopy--] = data[i--];
count += j- start- length;
}else{
copy[indexCopy--] = data[j--];
}
}
for(; i>=start; --i){
copy[indexCopy--] = data[i];
}
for(; j>= start+length+1; --j){
copy[indexCopy--] = data[j];
}
return left+right+count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] data = {7,5,6,4};
InversePairs inversePairs = new InversePairs();
System.out.println(inversePairs.inversePairs(data));
}
}
剑指Offer 36题 数组中的逆序对 Java版 这个和归并排序一起看 对比差别在哪
最新推荐文章于 2022-04-04 13:57:17 发布