The title describes the two numbers in the array, and if the previous number is greater than the number that follows, the two numbers form an inverse pair. Enter an array to find the total number of reverse pairs in this array.
1 classSolution {2 Public:3 intInversepairscore (vector<int> &data,vector<int>©,intStartintend)4 {5 if(start==end)6 {7copy[start]=Data[start];8 return 0;9 }Ten intLength = (end-start)/2; One intleft = Inversepairscore (copy,data,start,start+length); A intright = Inversepairscore (copy,data,start+length+1, end); - - inti = start +length; the intj =end; - intIndexcopy =end; - intCount=0; - while(i>=start&&j>=start+length+1) + { - if(data[i]>Data[j]) + { Acopy[indexcopy--]=data[i--]; atcount+=j-start-length; - } - - Else - { -copy[indexcopy--]=data[j--]; in } - } to for(; i>=start;--i) +copy[indexcopy--] =Data[i]; - for(; j>=start+length+1;--j) thecopy[indexcopy--] =Data[j]; * returnleft+right+count; $ }Panax Notoginseng intInversepairs (vector<int>data) { - intLength =data.size (); the if(length<=0) + return 0; Avector<int>copy; the for(intI=0; i<length;i++) + Copy.push_back (Data[i]); - intCount = Inversepairscore (Data,copy,0, length-1); $ copy.clear (); $ returncount; - } - the -};
Interview Question 36 reverse order in the array