这是我在研究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版归并算法的更多相关文章

  1. leetcode315 Count of Smaller Numbers After Self

    思路: bit + 离散化. 实现: #include <bits/stdc++.h> using namespace std; class Solution { public: int ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

  4. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  5. [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 ...

  6. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  7. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. NOIP模拟:能源(二分答案)

    题目描述 小美为了拯救世界能源危机,她准备了 n 台蓄电池.一开始每台蓄电池有 ai 个单位的能量. 现在她想把 n 台蓄电池调整到能量相同.对于每台蓄电池可以给另一台蓄电池传递能量.但是会有能量损耗 ...

  2. HDU1035 Robot Motion

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  3. c3p0数据库连接池+mysql数据库基本使用方法

           一般我们在项目中操作数据库时,都是每次需要操作数据库就建立一个连接,操作完成后释放连接.因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒), 连接就会被自动释放掉. ...

  4. Redis主从复制(Master/Slave)

    Redis主从复制(Master/Slave) 修改配置文件 拷贝多个redis.conf文件分别配置如下参数: 开启daemonize yes pidfile port logfile dbfile ...

  5. Uva 548 二叉树的递归遍历lrj 白书p155

    直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include &l ...

  6. nessus重置密码

    许久不用的nessus密码居然忘记了,查了下: cmd下进入到nessus的安装目录 提升为管理员,登录系统 如果想用之前的账号,可以直接在系统内重置密码.

  7. 干货--Excel的表格数据的一般处理和常用python模块。

    写在前面: 本文章的主要目的在于: 介绍了python常用的Excel处理模块:xlwt,xlrd,xllutils,openpyxl,pywin32的使用和应用场景. 本文只针对于Excel表中常用 ...

  8. Git分支使用心得

    在去年的大约这个时候,我的领导让我研究一下git的使用方法,方便我们自己的代码管理,因为我们原先使用的是SVN,使用起来没那么方便,所以让我研究研究git的使用.我就简单的研究了两天,用我的IDE(v ...

  9. [NOIP2015] 斗地主 大爆搜

    考试的时候想了半天,实在是想不到解决的办法,感觉只能暴力..然后暴力也懒得打了,小数据模拟骗30分hhh 然而正解真的是暴力..大爆搜.. 然后我的内心拒绝改这道题(TAT) 不过在wcx大佬的帮助下 ...

  10. 关于他们回答的 "怎样在桌面建一个python GUI的快捷方式" 这个问题

    在之前的2个随笔里面,有写过<找到可以解决问题的正确的人>.<如何提问>,说白了就是您需要帮助的时候,您得让对方100%懂你,否则没戏. 那么最近看到这样1个古老的问题,和一些 ...