剑指offer——54数组中的逆序对
题目描述
输入描述:
题目保证输入的数组中没有的相同的数字
数据范围:
对于%50的数据,size<=10^4
对于%75的数据,size<=10^5
对于%100的数据,size<=2*10^5
输入
1,2,3,4,5,6,7,0
输出
7 题解:
这道题有待琢磨。。。
过程总结:
先把数组分隔成子数组,统计出子数组内部的逆序对的数目,然后再统计出两个相邻子数组之间的逆序对的数目。
//最笨的方法
class Solution01 {
public:
int InversePairs(vector<int> data) {
if (data.size() < )return ;
set<int>s;
s.insert(data[]);
int res = ;
for (int i = ; i < data.size(); ++i)
{
if (data[i] < *(s.begin()))
res += s.size();
else if (data[i] > *(--s.end()))
res += ;
else
{
int k = ;
for (auto ptr = s.begin(); ptr != s.end(); ++ptr, ++k)
{
if (*ptr > data[i])
{
res += s.size() - k;
break;
}
}
}
s.insert(data[i]);
}
return res;
}
}; //书本代码,有点乱
class Solution02 {
public:
int InversePairs(vector<int> data) {
if (data.size() < )return ;
vector<int>v;//用来复制的
v = data;
return InversePairsCore(data, v, , data.size() - );
} int InversePairsCore(vector<int>&data, vector<int>©, int start, int end)
{
if (start == end)
{
copy[start] = data[start];
return ;
} int length = (end - start) / ; int left = InversePairsCore(copy, data, start, start + length) % ;
int right = InversePairsCore(copy, data, start + length + , end) % ; // i初始化为前半段最后一个数字的下标
int i = start + length;
// j初始化为后半段最后一个数字的下标
int j = end;
int indexCopy = end;
int count = ;
while (i >= start && j >= start + length + )
{
if (data[i] > data[j])
{
copy[indexCopy--] = data[i--];
count += j - start - length;
if (count >= )//数值过大求余
{
count %= ;
}
}
else
{
copy[indexCopy--] = data[j--];
}
} for (; i >= start; --i)
copy[indexCopy--] = data[i]; for (; j >= start + length + ; --j)
copy[indexCopy--] = data[j]; return (left + right + count) % ;
}
}; //使用归并排序思想
class Solution03 {
private:
int count = ;
public:
int InversePairs(vector<int> data) {
if (data.size() < )return ;
mergeSort(data, , data.size() - );
return count;
}
void mergeSort(vector<int>&data, int L, int R)
{
if (L < R)
{
int M = (L + R) / ;
mergeSort(data, L, M);
mergeSort(data, M + , R);
merge(data, L, M, R);
}
}
void merge(vector<int>&data, int L, int M, int R)
{
vector<int>temp(R - L + );
int t = R - L;
int tL = M;
int tR = R;
while (tL >= L && tR >= M + )
{
if (data[tL] > data[tR])
{
count += tR - M;
temp[t--] = data[tL--];
count %= ;
}
else
temp[t--] = data[tR--];
}
while (tL >= L)
temp[t--] = data[tL--];
while (tR >= M + )
temp[t--] = data[tR--];
for (int i = ; i <= R - L; ++i)
data[L + i] = temp[i];
}
};
剑指offer——54数组中的逆序对的更多相关文章
- 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组
剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...
- 【Java】 剑指offer(51)数组中的逆序对
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成 ...
- 【剑指offer】数组中的逆序对
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mmc_maodun/article/details/27520535 转载请注明出处:http:// ...
- Go语言实现:【剑指offer】数组中的逆序对
该题目来源于牛客网<剑指offer>专题. 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对10000 ...
- 【剑指offer】数组中的逆序对。C++实现
原创文章,转载请注明出处! 博客文章索引地址 博客文章中代码的github地址 # 题目 # 思路 基于归并排序的思想统计逆序对:先把数组分割成子数组,再子数组合并的过程中统计逆序对的数目.统计逆序对 ...
- 剑指Offer 35. 数组中的逆序对 (数组)
题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...
- [剑指Offer] 35.数组中的逆序对
题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...
- 剑指offer:数组中的逆序对
题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%100 ...
- 微软面试题:剑指 Offer 51. 数组中的逆序对 Hard 出现次数:3
题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对. 输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出: 5 限制: ...
随机推荐
- Redis 5.0.7 讲解,单机、集群模式搭建
Redis 5.0.7 讲解,单机.集群模式搭建 一.Redis 介绍 不管你是从事 Python.Java.Go.PHP.Ruby等等... Redis都应该是一个比较熟悉的中间件.而大部分经常写业 ...
- PHP面试 MySQL数据库基础
MySQL数据库基础 MySQL数据类型 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT 属性:UNSIGNED 长度:可以为整数类型指定宽度,列 ...
- 畜禽免疫系统使用LODOP打印
<div class="btn_box"> <asp:Button ID="btnPrint" Text="预览并打印" ...
- Laravel4 最佳学习代码以及资料推荐(转)
https://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site 充分展现了Laravel的强大之处 Laravel虽然上手难度会比其他框架大很 ...
- Linux利器 strace [看出process呼叫哪個system call]
Linux利器 strace strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必 ...
- linux nohup python 后台运行无输出问题
参考:https://blog.csdn.net/zj360202/article/details/78894512 nohup python test.py & nohup python t ...
- 2018-2-13-win10-uwp-改变鼠标
title author date CreateTime categories win10 uwp 改变鼠标 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...
- Linux解压rar文件
Linux解压rar文件(unrar安装和使用,分卷解压) windows平台很多压缩文档为rar文件,那么怎么做到Linux解压rar文件(unrar安装和使用)? 简单,centos5安装unra ...
- Vmware虚拟机中安装ubuntu18 live server+Vmware Tools(用来共享本地文件夹)
一.安装Ubuntu见链接 https://ywnz.com/linuxaz/3696.html 二.安装Vmware Tools 参考:https://blog.csdn.net/a12340123 ...
- 检查目录下 文件的权限-linux shell脚本
#!/bin/bash #History: #2019/07/23 Fsq #This Program will check Permissions on dir PATH=/bin:/sbin ...