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?
 class Solution {
public:
void sortColors(vector<int>& nums) {
int i = , j = , k = nums.size() - ;
while (j <= k) {
if (nums[j] == ) {
swap(nums[i++], nums[j++]);
} else if (nums[j] == ) {
swap(nums[k--], nums[j]);
} else {
j++;
}
}
}
};

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 in-place so that objects of the ...

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

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

  4. Leetcode 75. Sort Colors

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

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

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

  6. 75. Sort Colors(荷兰国旗问题 三指针)

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

  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 计数排序,三路快排

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

随机推荐

  1. 后盾网lavarel视频项目---vue实现动态添加和删除板块

    后盾网lavarel视频项目---vue实现动态添加和删除板块 一.总结 一句话总结: 原理就是:列表时根据vue中的videos变量中的元素来遍历的,初始时videos:[{title:'',pat ...

  2. spark 2015 发展方向

    https://www.youtube.com/watch?v=YWppYPWznSQ    Databrick的spark 2015规划聚会的视频,看起来今年会在SQL和MLlib上发力多一点.   ...

  3. android 播放音乐媒体文件(一)

    Audio formats and codecs Format / Codec Encoder Decoder Details Supported File Type(s) / Container F ...

  4. SpringBoot深入了解

    核心配置文件 application 配置文件,主要用于 Spring Boot 项目的自动化配置. bootstrap 配置文件有以下几个应用场景. 使用 Spring Cloud Config 配 ...

  5. Android的工程目录主要有哪些

    src 源文件gen 生成的文件 R 文件就在此android. jar 依赖的 android sdkassets 资源文件bin 生成的字节码 apk 在此libs 依赖 jar 和 sores ...

  6. rocketMQ retry 消息的实现

    consumer 消费失败,会把消息重新发往 %RETRY% + consumerGroup,这个 retry 消息会在一定时间后,真实送到 retry topic. broker 处理发送到 ret ...

  7. flask包request获取参数

    原博文:https://www.cnblogs.com/wangjikun/p/6935592.html request.method #获取请求方法request.form #获取post请求所有参 ...

  8. 阶段3 2.Spring_05.基于XML的IOC的案例1_1 基于XML的IOC的案例-案例准备

    导坐标 创建数据库表 create table account( id int primary key auto_increment, name varchar(40), money float )c ...

  9. 使用Nginx做WebSockets代理教程

    WebSocket 协议提供了一种创建支持客户端和服务端实时双向通信Web应用程序的方法.作为HTML5规范的一部分,WebSockets简化了开发Web实时通信程 序的难度.目前主流的浏览器都支持W ...

  10. rsync启动脚本

    rsync启动脚本 01 #!/bin/bash  www.ahlinux.com 02 # 03 # rsyncd      This shell script takes care of star ...