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. dotnet core 发布配置(测试数据库和正式数据库自动切换)

    一.起源 在进行项目开发时,常常要求开发环境,测试环境及正式环境的分离,并且不同环境运行的参数都是不一样的,比如监听地址,数据库连接信息等.当然我们把配置信息保存到一个文件中,每次发布的时候,可以先修 ...

  2. ArrayList<HashMap<String,Object>>集锦

    1.   Android中如何从一个Activity中ArrayList<HashMap<String,Object>>传递到另一个activity?      eg:     ...

  3. 2013款MacBook Air装Windows7单系统

    经过两天的摸索,查找无数资料终于把2013款的MacBook Air装上了WIN 7,虽然网上有很多的资料但是都不是我想要的,第一个我的是2013款的MacBook Air,跟原来2012 11款Ma ...

  4. OpenCV:OpenCV目标检测Adaboost+haar源代码分析

    使用OpenCV作图像检测, Adaboost+haar决策过程,其中一部分源代码如下: 函数调用堆栈的底层为: 1.使用有序决策桩进行预测 template<class FEval> i ...

  5. Reducing the Dimensionality of Data with Neural Networks:神经网络用于降维

    原文链接:http://www.ncbi.nlm.nih.gov/pubmed/16873662/ G. E. Hinton* and R. R. Salakhutdinov .   Science. ...

  6. 迭代器与index遍历

    迭代器用于链式组织的序列. index用于线性组织的序列.

  7. jquery.datatable.js实际运用

    $.dataTablesSettings = { deferRender: true,// 当处理大数据时,延迟渲染数据,有效提高Datatables处理能力 bStateSave: true,//表 ...

  8. day001 Python 计算机基础(2019年5月16日)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp作为一名程序员或者即将踏入IT行业的准程序员,学习任何一门编程语言,都需要有基本的计算机基础 ...

  9. BOS工具之BOS应用框架

    大纲:    应用框架概述,bos应用框架总体,bos应用框架详细设计,代码结构以及常用应用,开发常用接口 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象组件及组件实例间交互的 ...

  10. win10环境下配置django+Apache2.4.38+python3.6项目

    1.)Apache-2.4.38-win64-vc15下载地址: https://www.apachelounge.com/download/VC14/ 解压httpd-2.4.38-win64-VC ...