当前位置:网站首页>《剑指Offer》数组中的逆序对
《剑指Offer》数组中的逆序对
2022-07-27 14:15:00 【傻子是小傲娇】
这几天烦心的事一直来,真不知道该怎样才好,一个归并排序求逆序对也能从昨晚卡到现在,反正也睡不着,debug到凌晨,今早才发现一个传参数的问题,应该传引用。改了还是过不了,想了半天才想到数据溢出的问题,归并过程中也可能溢出的。希望一切都会好起来,哎。
题目描述
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
如序列:3 , 6, 8, 4, 5, 7的逆序对为5 6-4 6-5 8-4 8-5 8-7
详解:https://blog.csdn.net/love_phoebe/article/details/81231546
AC代码:
class Solution { public: int count, cnt, Mod; void Merge(int left, int mid, int right, vector<int>&data, vector<int>&temp){ int i = left; int j = mid + 1; int k = left; while (i <= mid && j <= right){ if (data[i] <= data[j])temp[k++] = data[i++]; else { temp[k++] = data[j++]; count += mid - i + 1; count %= Mod; } } while (i <= mid)temp[k++] = data[i++]; while (j <= right)temp[k++] = data[j++]; for (cnt = left; cnt <= right; cnt++)data[cnt] = temp[cnt]; } void MergeSort(int left, int right, vector<int>&data, vector<int>&temp){ if (left < right){ //cout <<"M"<<left << " " << right << endl; int mid = (left + right) / 2; MergeSort(left, mid, data, temp); MergeSort(mid + 1 ,right, data, temp); Merge(left, mid, right, data, temp); } } int InversePairs(vector<int> data) { count = 0; Mod = 1000000007; int length = data.size(); if (length <= 0)return 0; vector<int>temp; for (int i = 0; i < length; i++)temp.push_back(0); vector<int>& a = data; vector<int>& b = temp; MergeSort(0, length - 1, a, b); return count % Mod; } };
边栏推荐
- Skywalking distributed system application performance monitoring tool - medium
- 网络设备硬核技术内幕 路由器篇 20 DPDK (五)
- The interviewer asked: how to judge whether an element is in the visible area?
- NEFU118 n! How many zeros are there after [basic theorem of arithmetic]
- 【云享读书会第13期】视频文件的封装格式
- Kubernetes CNI 分类/运行机制
- 泛型
- 3.3-5v转换
- The mobile terminal uses the list component of vantui. When multiple tab items are switched back and forth, the list is loaded many times, resulting in the failure of normal display of data
- LeetCode 81. 搜索旋转排序数组 II 二分/medium
猜你喜欢

ADB command (install APK package format: ADB install APK address package name on the computer)

Idea makes jar packages and introduces jar packages

图解 SQL,这也太形象了吧

【ManageEngine】什么是SIEM

Differences among CPU, GPU and NPU

Disk troubleshooting of kubernetes node

Jmeter录制接口自动化

LeetCode 190. 颠倒二进制位 位运算/easy

LeetCode 781. 森林中的兔子 哈希表/数学问题 medium

反射
随机推荐
Getting started with DirectX
Wechat applet realizes music search page
修改frameworks资源文件如何单编
网络设备硬核技术内幕 路由器篇 9 CISCO ASR9900拆解 (二)
[Yunxiang book club issue 13] multimedia processing tool ffmpeg tool set
网络设备硬核技术内幕 路由器篇 21 可重构的路由器
cap理论和base理论
LeetCode 456. 132模式 单调栈/medium
网络设备硬核技术内幕 路由器篇 13 从鹿由器到路由器(上)
移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示
web上构建3d效果 基于three.js的实例
被动收入:回归原始且安全的两种赚取方法
LeetCode 240. 搜索二维矩阵 II medium
《剑指Offer》剪绳子
Automatically configure SSH password free login and cancel SSH password free configuration script
Disk troubleshooting of kubernetes node
基于FIFO IDT7202-12的数字存储示波器
LeetCode 81. 搜索旋转排序数组 II 二分/medium
What is tor? What is the use of tor browser update?
代码覆盖率统计神器-jacoco工具实战