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.

 void sortColors(int* nums, int numsSize)
{
if(numsSize==||nums==NULL)
return; if(numsSize==)
return; int red=,white=,blue=; for(int i=;i<numsSize;i++)
{
if(nums[i]==)
red++; if(nums[i]==)
white++; if(nums[i]==)
blue++;
}
for(int i=;i<numsSize;i++)
{
if(i<red)
{
nums[i]=;
}
if(i>=red&&i<(red+white))
{
nums[i]=;
}
if(i>=(red+white)&&i<numsSize)
{
nums[i]=;
} }
}

LeeCode-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. Sort Colors && Combinations

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

  3. Lintcode: 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: Sort Colors II 解题报告

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

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

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

  6. Sort Colors I & II

    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 (3 solutions)

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

  8. LeetCode: 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 & Set Matrix Zeroes & Sort Colors

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

  10. [Leetcode Week2]Sort Colors

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

随机推荐

  1. Uva227.Puzzle

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. 初探JS正则表达式

    1.概述     正则表达式是一个描述字符模式的对象.Javascript的正则表达式语法的是Perl5的正则表达式的子集.JS正则表达式有两种使用方式,文本模式和RegExp对象模式,实例如下: v ...

  3. nyoj 36 最长公共子序列

    描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subseque ...

  4. CI分支kohana在线文档

    http://www.luzhongpeng.com/guide/kohana/mvc/controllers http://www.lampblog.net/kohana3%E4%BD%BF%E7% ...

  5. [HeadFirst-HTMLCSS入门][第十一章布局排版]

    流 浮动布局 冻结布局 凝胶布局 绝对布局 表格显示布局 postion 绝对 静态 固定 相对 浮动元素 必须指明宽度 解决重合 中缝 设置外边距留中缝,好看一点 clear标签 不准左右有浮动元素 ...

  6. stagefright omx小结

    由于stagefright和openmax运行在两个不同的进程上,所以他们之间的通讯要经过Binder进行处理,本小结不考虑音频这一块,假设视频为MP4封装的AVC编码文件. 先简单的看一下stage ...

  7. Linux中oracle安装时候报ora-00119解决办法

    ORA-00119: invalid specification for system parameter LOCAL_LISTENER ORA-00130: invalid listener add ...

  8. 本地计算机上的OracleOraDb11g_home2TNSListener服务启动又停止了。

    电脑上装了oracle后启动很慢,然后我就不oracle服务设置成手动启动,没想到今天启动的时候居然报错 折腾了一上午,终于搞定, 在环境变量中把ORACLE_HOME 设置成D:\app\XL\pr ...

  9. tableView特色用法

    // //  ViewController.m //  UITableView // //  Created by yhj on 15/12/15. //  Copyright © 2015年 QQ: ...

  10. (转) mac 下的eclipse 配置 python 2.7

    原地址: http://marsfreewill.blogspot.it/2012/08/mac-ospythonpydeveclipse.html   在MAC OS上配置Python开发环境(Py ...