Given an array with n objects colored red, white or blue, sort them 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.

void sortColors(vector<int>& nums)
{//排序时间复杂度O(n)
int length=nums.size();
int zero=0;
int two=length-1;
while(zero<length && nums[zero]==0)
{//找到第一个不为0的数的下标
zero++;
}
while(two>=zero && nums[two]==2)
{//找到倒数第一个不为2的数的下标,zero前的数都是0
two--;
} for(int i=zero;i<=two;++i)
{
if(nums[i]==0)
{//假设为0,和zero指向的数交换,zero前移
nums[i]=1;//zero指向的值肯定是1,首先不可能为0,其次,i遍历过的地方,2都已经换到最后
nums[zero]=0;
zero++;
}
else if(nums[i]==1)
{//假设为1,则跳过
continue;
}
else
{//假设为2。则和two指向的数交换,并更新two的值。
//和zero不同,two前面的数可能还未排序
nums[i]=nums[two];
nums[two]=2;
two--;
while(two>=i && nums[two]==2)
{
two--;
}
i--;//先向后退一步。由于two指向的数还未进行排序
}
}
}

leetCode(30):Sort Colors的更多相关文章

  1. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

  2. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  3. LeetCode 75. Sort Colors (颜色分类):三路快排

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

  4. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

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

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

  6. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

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

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

  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】Sort Colors(middle)☆

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

随机推荐

  1. 一个能让cin和scanf 一样快的方法:

    cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说这两种方法可以混用,而不必担心文件指针混乱,同时cout和stdout也一样,两者混用不会输出顺序错乱.正因为这个兼容性的 ...

  2. LeetCode Weekly Contest 28

    1. 551. Student Attendance Record I 2. 552. Student Attendance Record II hihocode原题,https://hihocode ...

  3. 上传预览图片的插件jquery-fileupload

    上传预览图片的插件jquery-fileupload github地址:https://github.com/blueimp/jQuery-File-Upload 中文文档:http://www.jq ...

  4. eclipse中server 没有tomcat选项

    eclipse集成Tomcat: 打开eclipse - 窗口 - 首选项 - 服务器 - 运行时环境 找到Tomcat然后添加. eclipse添加插件: 开发WEB项目时要集成Tomcat可以并不 ...

  5. 【Oracle】解锁用户

    登录oracle数据库时有时会显示ERROR: ORA-28000: the account is locked,这是因为所登录的账号被锁定了. 解决办法: sqlplus / as sysdba; ...

  6. 时序分析:串匹配—Brute-Force算法

    在使用KMP算法之前,使用了BF算法用于串匹配:原文链接已无法查找.....        设有主串s和子串t,子串t的定位就是要在主串s中找到一个与子串t相等的子串.通常把主串s称为目标串,把子串t ...

  7. msmq消息队列使用场景

    MSMQ全称是Microsoft Message Queue——微软消息队列. MSMQ是一种通信的机制,因为是一种中间件技术,所以它能够支持多种类型的语言开发,同时也是跨平台的通信机制,也就是说MQ ...

  8. 点击button 触发另一个button 事件

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs&quo ...

  9. jq 获取表单所有数据

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. react-router @4用法整理

    在React Router 3上写了一篇文章后不久,我第一次在React Rally 2016上遇到了Michael Jackson.Michael是React Router和Ryan Florenc ...