当前位置:网站首页>【剑指Offer】51. 数组中的逆序对
【剑指Offer】51. 数组中的逆序对
2022-06-29 19:31:00 【LuZhouShiLi】
剑指 Offer 51. 数组中的逆序对
题目
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。
思路
K神的题解:https://leetcode.cn/problems/shu-zu-zhong-de-ni-xu-dui-lcof/solution/jian-zhi-offer-51-shu-zu-zhong-de-ni-xu-pvn2h/
代码
class Solution {
public:
int reversePairs(vector<int>& nums) {
vector<int> tmp(nums.size());
return mergeSort(0,nums.size() - 1,nums,tmp);
}
private:
int mergeSort(int l,int r, vector<int>& nums, vector<int>& tmp)
{
// 终止条件
if(l >= r) return 0;
// 递归划分
int m = (l + r) / 2;
int res = mergeSort(l,m ,nums,tmp) + mergeSort(m + 1,r ,nums,tmp);// 归并排序
// 合并阶段
int i = l,j = m + 1;
// 保存数组合并之前的模样
for(int k = l; k <= r; k++)
{
tmp[k] = nums[k];
}
for(int k = l; k <= r; k++)
{
// m及左边元素合并完成 把右边剩下的放入合并之后的数组
if(i == m + 1)
{
nums[k] = tmp[j ++];
}
else if(j == r + 1 || tmp[i] <= tmp[j])
{
// m + 1及右边的元素合并完成 将左边剩下的元素放入合并之后的数组
// 或者左边数组的元素小于等于右边,将左边数组的元素放入结果数组中,并让索引i加1
nums[k] = tmp[i ++];
}
else
{
// 右边数组的元素小于左边,将右边数组的元素放入结果数组中,并让索引+1
// 并且此时左边数组中的从i到m所有数都是大于tmp[j]的(因为左边的数组已经排序完毕)
nums[k] = tmp[j++];
res += m - i + 1;// 统计逆序对
}
}
return res;
}
};
边栏推荐
- [software testing] 01 -- software life cycle and software development model
- 小米笔试真题一
- From CIO to Consultant: the transformation of it leaders
- Various API methods of selenium
- How important is it to make a silver K-line chart?
- Qui vole dans un jeu d'écriture?
- Escape and March, the "two-sided Jianghu" of temporary food
- JVM (3) class loading
- One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform
- 一小时构建示例场景 声网发布灵隼物联网云平台
猜你喜欢

MBA-day26 数的概念与性质
![[笔记]再笔记--边干边学Verilog HDL –008](/img/7f/0ca73446247455ac4d8f9667083a87.png)
[笔记]再笔记--边干边学Verilog HDL –008

JVM(4) 字節碼技術+運行期優化

Game Maker 基金会呈献:归属之谷

One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform

Classic illustration of K-line diagram (Collection Edition)

习题8 #第8章 Verilog有限状态机设计-4 #Verilog #Quartus #modelsim

【笔记】再笔记--边干边学Verilog HDL – 014

微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条

建立自己的网站(12)
随机推荐
【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace
Physical verification LVS process and Technology (Part I)
In 2022, the financial interest rate has dropped, so how to choose financial products?
深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
Solution of portable emergency communication system for flood control and rescue
4-2 port banner information acquisition
DAO 中存在的不足和优化方案
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
7.取消与关闭
创作者基金会 6 月份亮点
Why is informatization ≠ digitalization? Finally someone made it clear
How is the combination of convolution and transformer optimal?
细说GaussDB(DWS)复杂多样的资源负载管理手段
揭秘!付费会员制下的那些小心机!
Oracle11.2.0.4-Rac集群hang分析记录
Win11安装权限在哪里设置?Win11安装权限设置的方法
Product axure9 (English version), repeater implements addition, deletion, query and modification of table contents (crud)
STM32CubeMX 学习(6)外部中断实验
软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线