【剑指offer】题36:数组中的逆序对
来源:互联网 发布:淘宝有质量问题投诉 编辑:程序博客网 时间:2024/05/19 03:43
int InversePairs_Core(vector<int>& data, vector<int>& copy, int left, int right){ if (left>=right) return 0; int mid = left + ((right - left) >> 1); int leftNum = InversePairs_Core(copy, data, left, mid); int rightNum = InversePairs_Core(copy, data, mid + 1, right); int count(0), i(right), left_index(mid), right_index(right); while (left_index>=left&&right_index>=mid+1) { if (data[left_index]>data[right_index]) { count += right_index-mid; if (count>1000000007) { count %= 1000000007; } copy[i--] = data[left_index--]; } else { copy[i--] = data[right_index--]; } } while (left_index>=left) { copy[i--] = data[left_index--]; } while (right_index>=mid+1) { copy[i--] = data[right_index--]; } return (count + leftNum + rightNum) % 1000000007;}int InversePairs(vector<int>& data){ if (data.size() < 2) return 0; vector<int> copy(data); return InversePairs_Core(data, copy, 0, data.size() - 1);}int main(){ vector<int> vec{ 7,5,6,4 }; cout<<InversePairs(vec);#ifdef debug_#else#endifreturn 0;}
阅读全文
0 0
- 剑指offer 36 数组中的逆序对
- 剑指offer 36 - 数组中的逆序对
- 《剑指offer》:[36]数组中的逆序对
- 剑指offer 36 数组中的逆序对
- 剑指offer-36-数组中的逆序对
- 【剑指offer】题36:数组中的逆序对
- 【剑指offer】数组中的逆序对
- 剑指offer--数组中的逆序对
- 剑指Offer之 - 数组中的逆序对
- 剑指offer之数组中的逆序对
- 《剑指offer》数组中的逆序对
- 【剑指offer】数组中的逆序对
- 剑指offer—数组中的逆序对
- 【剑指offer】之数组中的逆序对
- 剑指offer:数组中的逆序对
- [剑指offer]数组中的逆序对
- 剑指offer:数组中的逆序对
- 剑指Offer--036-数组中的逆序对
- elasticsearch5.4版本 head插件正确的打开方式。。。
- BZOJ 2190: [SDOI2008]仪仗队 欧拉函数
- C++递归算法之爬楼梯
- poi导出excel
- oracle查询优化
- 【剑指offer】题36:数组中的逆序对
- 四.python urllib2 ip伪装,cookie登陆获取,利用cookie进行登陆
- Scala学习笔记(一)入门前凑
- OpenGL中位图的操作(glReadPixels,glDrawPixels等)
- Shell输入输出重定向:Shell Here Document,/dev/null文件 .sh文件包含执行
- Eclipse快捷键大全
- C# 中的委托和事件
- 将多个小文件合并成大文件
- 学习makefile(三)--书写规则