当前位置:网站首页>[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;
}
};
边栏推荐
猜你喜欢
After CDN is added to the website, the Font Icon reports an error access control allow origin
虎符限币种提现 用户曲线出金即亏损
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
Win11安装权限在哪里设置?Win11安装权限设置的方法
电脑ssd硬盘怎么安装使用
Game maker Foundation presents: Valley of belonging
Technical methodology of new AI engine under the data infrastructure upgrade window
JVM (4) bytecode technology + runtime optimization
ChainSafe跨链桥部署教程
云服务器的安全设置常识
随机推荐
一个mysql里有3306端口下,一个mysql有20多个数据库,怎么一键备份20多个数据库,做系统备份,防止数据误删除?
3-2主机发现-三层发现
Common knowledge of ECS security settings
Escape and March, the "two-sided Jianghu" of temporary food
Flutter 调用百度地图APP实现位置搜索、路线规划
小米笔试真题一
Game maker Foundation presents: Valley of belonging
使用 OpenCV 的基于标记的增强现实
Qui vole dans un jeu d'écriture?
Inception 新结构 | 究竟卷积与Transformer如何结合才是最优的?
数据库是什么?数据库详细笔记!带你走进数据库~你想知道的这里都有!
数据安全解决方案的大时代
软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线
WPS和Excele
【Proteus仿真】矩阵键盘中断扫描
CAD Assistant - 3D模型格式转换利器
What about frequent network disconnection of win11 system? Solution to win11 network instability
细说GaussDB(DWS)复杂多样的资源负载管理手段
Test method learning
WPS and Excelle