题目

https://leetcode.com/problems/sort-colors/

题解

快速排序3.0(随机快排+荷兰国旗技巧优化)

在arr[L…R]范围上,进行快速排序的过程:

1)在这个范围上,随机选一个数记为num,

1)用num对该范围做partition,< num的数在左部分,== num的数中间,>num的数在右部分。假设== num的数所在范围是[a,b]

2)对arr[L…a-1]进行快速排序(递归)

3)对arr[b+1…R]进行快速排序(递归)

因为每一次partition都会搞定一批数的位置且不会再变动,所以排序能完成

随机快排的时间复杂度分析

在arr[L…R]范围上,进行快速排序的过程:

1)在这个范围上,随机选一个数记为num,

1)用num对该范围做partition,< num的数在左部分,== num的数中间,>num的数在右部分。假设== num的数所在范围是[a,b]

2)对arr[L…a-1]进行快速排序(递归)

3)对arr[b+1…R]进行快速排序(递归)

因为每一次partition都会搞定一批数的位置且不会再变动,所以排序能完成

class Solution {public void sortColors(int[] nums) {if (nums.length < 2) return;process(nums, 0, nums.length - 1);}public void process(int[] arr, int L, int R) {if (L >= R) return;swap(arr, L + (int) (Math.random() * (R - L + 1)), R); // 随机选一个数作为基准,放在最右侧int[] equalArea = netherlandsFlag(arr, L, R);process(arr, L, equalArea[0] - 1); // 小于区递归process(arr, equalArea[1] + 1, R); // 大于区递归}public int[] netherlandsFlag(int[] arr, int L, int R) {if (L > R) return new int[]{-1, -1};if (L == R) return new int[]{L, R};int less = L - 1; // 小于区的右边界int more = R; // 大于区的左边界int index = L; // 当前数,并且index左边的数一定都小于等于arr[R](pivot)while (index < more) {if (arr[index] == arr[R]) {index++;} else if (arr[index] < arr[R]) {swap(arr, index++, ++less);} else {swap(arr, index, --more);}}swap(arr, more, R);return new int[]{less + 1, more};}public void swap(int[] arr, int i, int j) {int t = arr[i];arr[i] = arr[j];arr[j] = t;}
}

leetcode 75. Sort Colors | 75. 颜色分类(荷兰国旗问题,快速排序)相关推荐

  1. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色.在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Given ...

  2. 【75】颜色分类--荷兰国旗问题

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...

  3. [LeetCode]75.Sort Colors

    [题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...

  4. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  5. 【排序】LeetCode 75. Sort Colors

    LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...

  6. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  7. Leetcode 075 Sort Colors

    题目连接:Leetcode 075 Sort Colors 解题思路:从左向右遍历,同时维护两个指针r和b,0~r位置上的元素为0,b~n-1位置上的为2.在遍历过程中,碰到0,放到r的位置:碰到2, ...

  8. LeetCode 75. Sort Colors

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  9. 个人记录-LeetCode 75. Sort Colors

    问题: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

最新文章

  1. CVPR 2020 论文开源项目一页看尽,附代码论文
  2. linux基本网络IP自动、手动配置
  3. loadlibrary failed with error 126:找不到指定模块
  4. Python Django开发案例:POST方式实现登录功能
  5. 深度学习(part1)--机器学习及深度学习基础
  6. Stm32:地址空间划分
  7. github本地库及clone常用命令
  8. Scala map中下划线_._2的含义
  9. react 动态修改路由_react动态路由以及获取动态路由
  10. jmeter录制 过滤_jmeter脚本录制方法:
  11. This property is obsolete now. Use the TextPattern property instead.的警告解决办法
  12. Attack State Slight Movement(攻击状态)
  13. vue 循环播放音乐列表 音量控制
  14. 无线网卡wifi无法启用
  15. 你为你的机会准备了什么
  16. Tcl/Tk入门(上)
  17. Lodash的一些基本使用
  18. 文本分类入门(一)文本分类问题的定义
  19. perl中grep用法总结 z
  20. JavaScript是多线程还是单线程?

热门文章

  1. m3u8文件在手机上用什么软件看_如何用手机从一个网页下载视频
  2. 最长上升子序列(LIS)的求法
  3. 彻底理解 Python 生成器
  4. 完成端口(Completion Port)详解----- By PiggyXP(小猪)
  5. Android 模拟器下载、编译及调试
  6. C++ STL : 模拟实现STL中的容器适配器stack和queue
  7. Scala与Java集合互转摘要
  8. 阿里技术官最新总结一份105道Java面试题小册,看完我惊呆了
  9. 深度学习为图片压缩算法赋能:节省55%带宽
  10. C/C++学习之路: 模板和异常