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 ...
随机推荐
- jenkins2 -pipeline 常用groovy脚本
jenkins2的核心是pipeline,pipeline的核心是groovy. 那有一些基础的groovy是必须经常使用的,如变量赋值,变量引用,打印变量,输出字符,任务调用,循环判断等. Groo ...
- 机器学习(4)Hoeffding Inequality--界定概率边界
问题 假设空间的样本复杂度(sample complexity):随着问题规模的增长导致所需训练样本的增长称为sample complexity. 实际情况中,最有可能限制学习器成功的因素是训练数据的 ...
- angularjs 动态显示内容适用于$modal
1.创建指令 angular.module('app').directive('dynamicElement', ["$compile", function ($compile) ...
- Asp.Net MVC-4-过滤器1:认证与授权
基础 过滤器体现了MVC框架中的Aop思想,虽然这种实现并不完美但在实际的开发过程中一般也足以满足需求了. 过滤器分类 依据上篇分析的执行时机的不同可以把过滤器按照实现不同的接口分为下面五类: IAu ...
- 一.CPU,Mem过高怎么办 --这是个开始
本身是名Java开发,在做了一段大数据的工作后,猛然间想对Java做个总结. 从未写过技术博客,一时不知如何开始,思虑后,暂且以自己喜爱的方式来开篇. 工作中遇到过CPU或内存过高的问题,解决步骤: ...
- 文科生细谈学习Linux系统的重要性
首先大概介绍下自己,我学的是公共事业管理方面的专业,可以说是面向纯理论,社区管理社会管理的专业,但是从大二开始,对网络及服务器运维方面产生浓厚兴趣,并不断在网上找相关资料. 在这期间经历过很多,单说桌 ...
- JavaScript字符串处理
字符串处理 1.连接字符串: 1)连接符+: 2)连接赋值+=: 3)连接函concat() 2.查找子串位置indexOf() 1)在指定字符串中是否存在给定的字符串(第一次出现) 2)用法str. ...
- hdu 6045 Is Derek lying?(思维推导)
Problem Description Derek and Alfia are good friends.Derek is Chinese,and Alfia is Austrian.This sum ...
- S2-032代码执行
Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与Struts 1的体系结构差别 ...
- ABAP中Collect的用法
vaule:collect在非数值字段相同的情况下,起到了数值字段汇总作用. 非数值字段不同的情况下,效果和append相同执行插入内表操作,当非数值字段相同的时候,则相当于modify的效果,只不过 ...