Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note: You are not suppose to use the library's sort function for this problem.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Follow up:

    • A rather straight forward solution is a two-pass algorithm using counting sort.
      First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
    • Could you come up with a one-pass algorithm using only constant space?

题目要求是,时间复杂度O(n) 空间复杂度O(1) 对于没有听过三路快排的窝来说,这个medium比hard难得多好趴。。。

 
先说原理,
 
快速排序就是找一个基准v,通过双向指针,把<v的值放在左边,>=v的值放在右边,然后递归。
 
三路,就是 <v, =v, >v。那么如何分成三分呢?
 
设有一个数组 [0...n] 遍历数组,index 表示遍历到的位置。
 
找两个分界位置,lt 和 rt 用来表示 =v 和 >v 的最小位置。
 
如果遇到小于 v 的就让其和lt交换,同时将 lt++。等于 v 不需要做操作。大于 v 就将 lt-- 其和 lt 作交换
 
之后将 <v 和 >v 的两段递归排序就可以啦。
 
 
那么这题,只是三路排序的一个思路~~
 
/*
* @lc app=leetcode id=75 lang=javascript
*
* [75] Sort Colors
*/
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
let n = nums.length;
let lt = 0, // =v 的第一个
rt = n; // >v 的第一个 let index = 0;
let v = 1; while (index < n && index < rt) {
if (nums[index] > v) {
rt--;
swap(index, rt);
} else if (nums[index] === v) {
index++;
} else if (nums[index] < v) {
swap(lt, index);
lt++;
index++;
}
} function swap(i, j) {
let t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
};

LeetCode 75. Sort Colors (颜色分类):三路快排的更多相关文章

  1. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  2. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

  3. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

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

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

  5. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

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

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

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

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

  8. Leetcode 75. Sort Colors

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

  9. [leetcode]75. Sort Colors三色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

随机推荐

  1. CentOS 8 换源,设置dnf / yum镜像

    aliyun更新了centos8的说明 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos- ...

  2. 2.将视图添加到 ASP.NET Core MVC 应用

    在本部分中,将修改 HelloWorldController 类,进而使用 Razor 视图文件来顺利封装为客户端生成 HTML 响应的过程. 当前,Index 方法返回带有在控制器类中硬编码的消息的 ...

  3. ABP中文网的一些BUG

    之前一些翻译了的文档没有及时更新.比如 IAsyncCrudAppService接口在很久之前的版本就已经改为了ICrudAppService,如果是在官网下载的最新实例中IAsyncCrudAppS ...

  4. 【maven】搭建maven私服--基于CentOS7.6+docker

    一.docker环境 Docker version 19.03.5, build 633a0ea 二.安装并启动 Maven 私服的工具: Sonatype Nexus 1.搜索 2.下载镜像 doc ...

  5. webpack加载css文件及其配置

    webpack加载css文件及其配置 当我们写了几个css文件之后如果想要引用到html中去的话我们最开始的方式就是通过link标签将css文件导入进去,但是如果我们的css文件有很多的话,一个个的导 ...

  6. 前端技术扫盲-rem和px以及em关系

    首先rem,em和px都是长度单位,下面单独介绍各个单位的概念: px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.PC端用比较多的. em是相对长度单位.相对于当前对象内 ...

  7. php中mysqli_error($conn)的用法

    注意:用函数判断sql语句是否有错需要把这个函数写在mysqli_query("$conn连接语句","$sql语句")后面才能进行检测. 返回值:返回最近调用 ...

  8. itextpdf5单元格中的段落没有行间距

    关于对表格中的段落没有行间距的解决方式:通过观察和推测的结论:itextpdf对一些属性,只会对最外层元素的属性进行接收处理,如行间距.例: Paragraph p = new Paragraph(1 ...

  9. Redis缓存实战教程

    目录 Redis缓存 使用缓存Redis解决首页并发问题 1.缓存使用的简单设计 2.Redis的整合步骤 A 将Redis整合到项目中(Redis+Spring) B 设计一个数据存储策越 3.Re ...

  10. Gitlab批量迁移项目

    最近接到一个需求,要把一个Gitlab上边的项目全部导入到另外一个Gitlab,借鉴了网上的一个方法,成功实现. 参考链接:https://segmentfault.com/a/11900000159 ...