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. javascript模块化编程的cmd规范(sea.js)

    CMD(Common Module Definition,通用模块定义)是一种模块定义规范,规范中明确了模块的基本书写格式和基本交互规则.SeaJS就是遵循的这个规范. define函数 在CMD规范 ...

  2. 汇总Anaconda与ROS冲突解决方法

    汇总一下在网上找到的Anaconda与ROS冲突解决方法,如果还有其他人找到其他方法,欢迎留言. anaconda和ros的安装就不介绍了. 1. 在某视频网站上一个印度小哥提出的方法 安装完成后,在 ...

  3. kali渗透综合靶机(五)--zico2靶机

    kali渗透综合靶机(五)--zico2靶机 靶机地址:https://www.vulnhub.com/series/zico2,137/#modal210download 一.主机发现 1.netd ...

  4. MySQL慢日志查询分析方法与工具

    MySQL中的日志包括:错误日志.二进制日志.通用查询日志.慢查询日志等等.这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1)通用查询日志:记录建立的客户端连接和执行的语句. 2)慢查 ...

  5. OfType<string>()

    object[] vals = { 1, "Hello", true, "World", 9.1 }; IEnumerable<double> ju ...

  6. 基于Spring Cloud Netflix的TCC柔性事务和EDA事件驱动示例

    Solar Spring Cloud为开发者提供了快速构建分布式系统中的一些常见工具,如分布式配置中心,服务发现与注册中心,智能路由,服务熔断及降级,消息总线,分布式追踪的解决方案等. 本次实战以模拟 ...

  7. JS初始

      简单只和复杂值的区别 1.简单值 简单值表示JS中可用的数据或信息的最底层简单形式. 注:简单之不可被细化. 也就是说,数字是数字,字符是字符,布尔值是true或false,null和undefi ...

  8. 基于Proxy的小程序状态管理

    摘要: 小程序状态管理. 作者:wwayne 原文:基于Proxy的小程序状态管理 Fundebug经授权转载,版权归原作者所有. 微信小程序的市场在进一步的扩大,而背后的技术社区仍在摸索着最好的实践 ...

  9. 「你学习,我买单」i春秋四周年精品课程福利专场

    i春秋:中国专业的网络安全在线教育平台,累计用户超过60万. i春秋课程:资深专家团队教研支持,人才培养结合企业需求,针对不同岗位和技术方向的课程总计:600+门,1500+章,5700+节,时长74 ...

  10. Struts2 OGNL表达式、ValueStack

    OGNL简介 OGNL,即Object-Graph Navigation Language,对象视图导航语言,是一种数据访问语言,比EL表达式更加强大: EL只能从11个内置对象中取值,且只能获取属性 ...