当前位置:网站首页>[sword finger offer] 51 Reverse pair in array
[sword finger offer] 51 Reverse pair in array
2022-06-29 19:36:00 【LuZhouShiLi】
The finger of the sword Offer 51. Reverse pairs in arrays
subject
Two numbers in an array , If the number in front is greater than the number in the back , Then these two numbers form a reverse order pair . Enter an array , Find the total number of reverse pairs in this array .
Ideas
K God's solution :https://leetcode.cn/problems/shu-zu-zhong-de-ni-xu-dui-lcof/solution/jian-zhi-offer-51-shu-zu-zhong-de-ni-xu-pvn2h/
Code
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)
{
// Termination conditions
if(l >= r) return 0;
// Recursive partition
int m = (l + r) / 2;
int res = mergeSort(l,m ,nums,tmp) + mergeSort(m + 1,r ,nums,tmp);// Merge sort
// Consolidation phase
int i = l,j = m + 1;
// Save what the array looks like before merging
for(int k = l; k <= r; k++)
{
tmp[k] = nums[k];
}
for(int k = l; k <= r; k++)
{
// m And the elements on the left are merged Put the rest on the right into the merged array
if(i == m + 1)
{
nums[k] = tmp[j ++];
}
else if(j == r + 1 || tmp[i] <= tmp[j])
{
// m + 1 And the elements on the right are merged Put the remaining elements on the left into the merged array
// Or the elements of the array on the left are less than or equal to those on the right , Put the elements of the left array into the result array , And let the index i Add 1
nums[k] = tmp[i ++];
}
else
{
// The elements of the array on the right are smaller than those on the left , Put the elements of the array on the right into the result array , And let the index +1
// And at this time, the from... In the left array i To m All numbers are greater than tmp[j] Of ( Because the array on the left has been sorted )
nums[k] = tmp[j++];
res += m - i + 1;// Statistical reverse order pair
}
}
return res;
}
};
边栏推荐
- The sales volume could not catch up with the speed of taking money. Weima went to Hong Kong for emergency rescue
- 软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线
- WPS and Excelle
- La collection numérique Meng xiangshun, artiste national du tigre peint, est disponible en quantité limitée et est offerte avec Maotai de l'année du tigre
- Go: how to write a correct UDP server
- 洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台
- ArrayList&lt;Integer&gt;使用==比较值是否相等出现 -129!=-129的情况思考
- What about frequent network disconnection of win11 system? Solution to win11 network instability
- KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering
- 凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
猜你喜欢
![[observation] softcom power liutianwen: embrace change and](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] softcom power liutianwen: embrace change and "follow the trend" to become an "enabler" of China's digital economy

3-3主机发现-四层发现

【U盘检测】为了转移压箱底的资料,买了个2T U盘检测仅仅只有47G~

JVM (2) garbage collection

From CIO to Consultant: the transformation of it leaders

Inception 新结构 | 究竟卷积与Transformer如何结合才是最优的?

La collection numérique Meng xiangshun, artiste national du tigre peint, est disponible en quantité limitée et est offerte avec Maotai de l'année du tigre

Win11系统频繁断网怎么办?Win11网络不稳定的解决方法

深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)

Violent solution to the question of guessing the ranking
随机推荐
JVM(3) 类加载
What about frequent network disconnection of win11 system? Solution to win11 network instability
startService() 过程
【Proteus仿真】矩阵键盘中断扫描
AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
Flutter 2.0 FocusScope.of(context).requestFocus(FocusNode()) 不生效的问题
go: 如何编写一个正确的udp服务端
【网络方向实训】-企业园区网络设计-【Had Done】
Why is informatization ≠ digitalization? Finally someone made it clear
Have you mastered all the testing methods of technology to ensure quality and software testing?
Common knowledge of ECS security settings
14,04 millions! Appel d'offres pour la mise à niveau de la base de données relationnelle et du système logiciel Middleware du Département des ressources humaines et sociales de la province du Sichuan!
软件测试逻辑覆盖相关理解
The era of data security solutions
1404万!四川省人社厅关系型数据库及中间件软件系统升级采购招标!
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
数据库是什么?数据库详细笔记!带你走进数据库~你想知道的这里都有!
【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace
Classic illustration of K-line diagram (Collection Edition)
C语言数组专题训练