Sort Colors

时间:2023-02-25 12:06:30

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.

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

分析: 一开始看到这个觉得肯定有非常简单有效地算法,采用多指针法,注意w,r,b的顺序,还是比较难想的

class Solution {
public:
void sortColors(vector<int>& nums) {
int r = , w = , b = ;
for (int i = ; i < nums.size(); i++) {
int num = nums[i];
cout << num << endl;
if (num == ) {nums[b++] = ; nums[w++] = ; nums[r++] = ; }
else if (num == ) {nums[b++] = ; nums[w++] = ;}
else if (num == ) b++;
}
}
};

  

Sort Colors的更多相关文章

  1. 【LeetCode】Sort Colors

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

  2. 52&period; Sort Colors &amp&semi;&amp&semi; Combinations

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

  3. Lintcode&colon; Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  4. Lintcode&colon; Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  5. 75&period; Sort Colors&lpar;颜色排序&rpar; from LeetCode

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

  6. Sort Colors I &amp&semi; II

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

  7. 【LeetCode】75&period; Sort Colors &lpar;3 solutions&rpar;

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

  8. LeetCode&colon; Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  9. LeetCode解题报告—— Rotate List &amp&semi; Set Matrix Zeroes &amp&semi; Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  10. &lbrack;Leetcode Week2&rsqb;Sort Colors

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

随机推荐

  1. https单向认证和双向认证

    单向认证: .clinet<--server .clinet-->server .client从server处拿到server的证书,通过公司的CA去验证该证书,以确认server是真实的 ...

  2. (转载)SQL去除回车符,换行符,空格和水平制表符

    http://www.cnblogs.com/insus/p/4815336.html MS SQL去除回车符,换行符,空格和水平制表符,参考下面语句,一般情况是SQL接受富文本或是textarea的 ...

  3. &period;&sol;upload&sol;source&sol;class&sol;class&lowbar;core&period;php

    定义了core这个类 error_reporting(E_ALL); error_reporting() 设置 PHP 的报错级别并返回当前级别.可以参考手册. define('IN_DISCUZ', ...

  4. vmware安装cent os 6&period;5 &plus; oracle 11g xe &plus; jboss eap 6&period;2 &plus; weblogic 12c&plus; webshpere mq 7&period;5

    前言: mac系统发展速度确实很快,短短数年,mac os上已经能网银支付(中行.招行.工商.支付宝等均已全面支持mac os了),windows上的经典常用软件:qq.飞信.旺旺.有道词典.有道云笔 ...

  5. EXPLAIN 命令详解

    在工作中,我们用于捕捉性能问题最常用的就是打开慢查询,定位执行效率差的SQL,那么当我们定位到一个SQL以后还不算完事,我们还需要知道该SQL的执行计划,比如是全表扫描,还是索引扫描,这些都需要通过E ...

  6. IIS 为应用程序池提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误的解决方法

    系统环境:Windows Server 2008 R2 64位, IIS 7.0 错误信息: 为应用程序池提供服务的进程在与 Windows Process Activation Service 通信 ...

  7. 转sklearn保存模型

    训练好了一个Model 以后总需要保存和再次预测, 所以保存和读取我们的sklearn model也是同样重要的一步. 比如,我们根据房源样本数据训练了一下房价模型,当用户输入自己的房子后,我们就需要 ...

  8. 集成学习之Boosting —— AdaBoost实现

    集成学习之Boosting -- AdaBoost原理 集成学习之Boosting -- AdaBoost实现 AdaBoost的一般算法流程 输入: 训练数据集 \(T = \left \{(x_1 ...

  9. Linux运维学习笔记-通配符及正则表达式知识总结

    通配符: * 代表所有   ? 任意一个字符   : 两个命令的分隔符   # 注释   | 管道,将|前命令的执行结果作为|后命令的输入   ~ 用户的家目录   - 上一次的目录   $ 变量前面 ...

  10. I&period;MX6 Manufacturing Tool V2 &lpar;MFGTool2&rpar; Update Command List &lpar;UCL&rpar; User Guide translate

    Manufacturing Tool V2 (MFGTool2) Update Command List (UCL) User Guide Contents(目录) Contents(目录)     ...