class Solution {
public:
int reversePairs(vectorint>& record) {
if(record.size()1)
return 0;
//归并 递归
int left,right;
left=0;
right=record.size()-1;
int num=mergeSort(left,right服务器托管网,record);
return num;
}
int mergeSort(int left,int right, vectorint>& record){
if(left>=right)
return 0;
int mid;
mid=left+(right-left)/2;
int leftnum=mergeSort(left,mid,record);
int rightnum=mergeSort(mid+1,right,record);
int mergenum=merge(left,right,mid,record);
return (leftnum+rightnum+mergenum);
}
int merge(int left,int right,int mid,vectorint>& record){
int num=0;
int i=left;
int j=mid+1;
vectorint> tmp;
while(imid && jright){
if(record[i]>record[j]){
num=mid-i+1+num;
tmp.push_back(record[j++]);
}
else
tmp.push_b服务器托管网ack(record[i++]);
}
while(imid){
tmp.push_back(record[i++]);
}
while(jright){
tmp.push_back(record[j++]);
}
for(int i=0;itmp.size();i++){
record[left+i]=tmp[i];
}
return num;
}
};
class Solution {
public:
int reversePairs(vectorint>& record) {
//归并 迭代
int num=0;
for(int step=1;steprecord.size();step*=2){
int leftmin,leftmax,rightmin,rightmax;
for(leftmin=0;leftmin+steprecord.size();leftmin=rightmax){
leftmax=rightmin=leftmin+step;
rightmax=leftmax+step;
if(rightmax>record.size())
rightmax=record.size();
int start=leftmin;
vectorint> tmp;
while(leftminleftmax && rightminrightmax){
if(record[leftmin]record[rightmin]){
tmp.push_back(record[leftmin++]);
}
else{
tmp.push_back(record[rightmin++]);
num = num+leftmax-leftmin;
}
}
while(leftminleftmax){
tmp.push_back(record[leftmin++]);
}
while(rightminrightmax){
tmp.push_back(record[rightmin++]);
}
for(int i=0;itmp.size();i++){
record[start+i]=tmp[i];
}
}
}
return num;
}
};
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
服务器托管网 文章目录 链接 图片引入 链接 准备工作 新建一个名为link.html和suc.html suc.html DOCTYPE html> html lang=”zh-CN”> head> meta charset=”UTF-8″…