当前位置:网站首页>[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;
}
};
边栏推荐
- NLP 类问题建模方案探索实践
- 自动获取本地连接及网络地址修改
- Static static member variables use @value injection
- ArrayList&lt;Integer&gt;使用==比较值是否相等出现 -129!=-129的情况思考
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- Flutter 调用百度地图APP实现位置搜索、路线规划
- 正则表达式系列之手机号码正则
- 4-2 port banner information acquisition
- Arm comprehensive computing solution redefines visual experience and powerfully enables mobile games
- php实现 提取不重复的整数(编程题目能够最快的熟悉函数)
猜你喜欢

From CIO to Consultant: the transformation of it leaders

npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed

idea中方法上没有小绿色三角

揭秘!付费会员制下的那些小心机!

逻辑结构与物理结构

虎符限币种提现 用户曲线出金即亏损

4-2端口Banner信息获取

【观察】软通动力刘天文:拥抱变化“顺势而为”,做中国数字经济“使能者”...

Win11 system component cannot be opened? Win11 system widget cannot be opened solution

JVM(2) 垃圾回收
随机推荐
罗清启:高端家电已成红海?卡萨帝率先破局
细说GaussDB(DWS)复杂多样的资源负载管理手段
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
电脑ssd硬盘怎么安装使用
4-2端口Banner信息获取
MBA-day26 数的概念与性质
ArrayList&lt;Integer&gt;使用==比较值是否相等出现 -129!=-129的情况思考
JVM (4) bytecode technology + runtime optimization
After CDN is added to the website, the Font Icon reports an error access control allow origin
小米笔试真题一
In 2022, the financial interest rate has dropped, so how to choose financial products?
idea中方法上没有小绿色三角
使用 OpenCV 的基于标记的增强现实
One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform
Test method learning
正则表达式系列之手机号码正则
3-3 host discovery - layer 4 discovery
MBA-day19 如果p则q矛盾关系p 且非q
软件测试逻辑覆盖相关理解
Freeswitch dial extension