LeetCode315—Count of Smaller Numbers After Self—Java版归并算法
这是我在研究leetcode的solution第一个解决算法时,自己做出的理解,并且为了大家能看懂,做出了详细的注释。
此算法算是剑指Offer36的升级版,都使用的归并算法,但是此处的算法,难度更高,理解起来更加费劲。
/*
* @Param res 保存逆变对数
* @Param index 保存数组下标索引值,排序数组下标值。
* 此算法使用归并算法,最大差异就在于merge()方法的转变
*
*
*/
public List<Integer> countSmaller(int[] nums) {
int[] res = new int[nums.length];
int[] index = new int[res.length];
for (int i = 0; i < res.length; i++) {
index[i] = i;
}
mergeSort(nums, index, 0, nums.length-1, res);
List<Integer> list = new LinkedList<>();
for (int i : res) {
list.add(i);
}
return list;
} private void mergeSort(int[] nums, int[] index, int l, int r, int[] res) {
if (l >= r) {
return;
}
int mid = (l+r)/2;
mergeSort(nums, index, l, mid, res);
mergeSort(nums, index, mid+1, r, res);
merge(nums, index, l, mid, mid+1, r, res);
}
/*
* 将左右两边排序好的数组进行逆序对计算,分别从左边起始处和右边起始处开始比较,
* 当,左边索引值大于右边时,count++,否则 左边索引值++;
* count值会一直保留,如果右边数组遍历到尾部,左边数组剩下的数的逆序数都会是count;
*
*
*
*/
private void merge(int[] nums, int[] index, int l1, int r1, int l2, int r2, int[] res) {
int start = l1;
int[] tmp = new int[r2-l1+1];
//记录逆序对数
int count = 0;
//temp数组的下标值
int p = 0;
while (l1 <= r1 || l2 <= r2) {
//左边数组遍历结束后,将右边剩余的值放到temp数组中,
if (l1 > r1) {
tmp[p++] = index[l2++];
//右边数组遍历结束后,将左边剩余的值放到temp数组中,
} else if (l2 > r2) {
//l1是原数组索引值,index[l1]是排序好的原数组中索引值。res[index[l1]]对应的原数组索引位置赋逆变数
res[index[l1]] += count;
tmp[p++] = index[l1++];
} else if (nums[index[l1]] > nums[index[l2]]) {
tmp[p++] = index[l2++];
count++;
} else {
//res存放每个数的最大值
res[index[l1]] += count;
tmp[p++] = index[l1++];
}
}
for (int i = 0; i < tmp.length; i++) {
//根据数组值排序,将对应的索引值放到index数组中。
index[start+i] = tmp[i];
}
}
LeetCode315—Count of Smaller Numbers After Self—Java版归并算法的更多相关文章
- leetcode315 Count of Smaller Numbers After Self
思路: bit + 离散化. 实现: #include <bits/stdc++.h> using namespace std; class Solution { public: int ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [LeetCode] 315. Count of Smaller Numbers After Self (Hard)
315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [Swift]LeetCode315. 计算右侧小于当前元素的个数 | Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new countsarray. The counts array has t ...
- LeetCode Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- LeetCode 315. Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- Count of Smaller Numbers After Self -- LeetCode
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The counts array has ...
随机推荐
- python函数(3):装饰器
昨天学了很多函数方面的概念和知识其中有一个闭包的函数.很多人都对闭包的作用不是很清楚,今天我们就来认识一个新的知识点装饰器.它就是闭包函数的一个经典应用. 预习: 编写装饰器,为多个函数加上认证的功能 ...
- Neo4j图数据库
01. 图数据库 图数据库是专门存储和检索大量信息网络的存储引擎.它可以有效地将数据存储为节点和关系,并允许高性能检索和查询这些结构.属性可以添加到节点和关系.节点可以用零个或多个标签标注,关系总是定 ...
- 假面舞会[NOI2008]
题目描述 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办方会把此编号告 ...
- Hadoop 新生报道(三) hadoop基础概念
一.NameNode,SeconderyNamenode,DataNode NameNode,DataNode,SeconderyNamenode都是进程,运行在节点上. 1.NameNode:had ...
- 记Angular与Django REST框架的一次合作(2):前端组件化——Angular
注:这是这个系列的第二部分,主要集中在Angular的使用方面.之前使用过AngularJS(Angular 1.x),混在Django的模板中使用,这些页面一般完全是结果展示页.在有Django表单 ...
- vue指令v-model示例解析
限制 <input> <select> <textarea> components 修饰符 .lazy - 取代 input 监听 change 事件 .numbe ...
- 初学Python之 字符串 索引 分片
字符串是字符的有序集合,可以通过其位置来获得具体的元素. 在python中,字符串中的字符是通过索引来提取的,索引从0开始. python可以取负值,表示从末尾提取,最后一个为-1,倒数第二个为-2, ...
- HTML的学习笔记
一.HTML的概述(了解) a.html是什么 : hypertext markup language 超文本标记语言 超文本:音频,视频,图片称为超文本.. ...
- NYOJ 57 6174问题
6174问题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,然后用a-b替 ...
- Appium环境搭建(Windows版)
Appium介绍 Appium是一个开源.跨平台的测试框架,可以用来测试原生及混合的移动端应用.Appium支持iOS.Android及FirefoxOS平台.Appium使用WebDriver的js ...