LeeCode-Sort Colors
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的更多相关文章
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 52. Sort Colors && Combinations
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【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 ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 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 ...
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
随机推荐
- 【HDU1514】Stars(树状数组)
绝对大坑.千万记住树状数组0好下标位置是虚拟节点.详见大白书P195.其实肉眼看也能得出,在add(有的也叫update)的点修改操作中如果传入0就会死循环.最后TLE.所以下标+1解决问题.上代码! ...
- OS error set
Failed to resolve/decode supposed IPv4 source addres Failed to resolve/decode supposed IPv4 source a ...
- jQuery ajax - getScript() 方法和getJSON方法
实例 使用 AJAX 请求来获得 JSON 数据,并输出结果: $("button").click(function(){ $.getJSON("demo_ajax_js ...
- ubuntu 14.04 下试用Sublime Text 3
很多源代码都没有IDE支持的,尤其是开源的源代码.从github上下载的,很多也不用IDE.包括我自己公司的代码,基本都是脚本,也不用IDE.通常情况下,都是用notepad++.UE之类的文本编辑器 ...
- Maven项目部署方案
以xbank项目做为应用背景,简单说明一下Maven项目的部署方案: 1.项目说明 xbank基础服务端的11个子项目均采用Maven搭建: 其中各项目功能设计如下: froad-xbank-serv ...
- UDP通讯协议
常见的通讯协议有udp和tcp. 先来简单了解一下这两个协议各自的特点: UDP: --将数据及源.目的封装在数据包中,不需要建立连接: --每个数据包的大小限制在64k以内: --因无连接,是不可靠 ...
- Microsoft Visual C++ 不支持long long
Microsoft Visual C++ 不支持long long 在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说, ...
- 写一个简易web服务器、ASP.NET核心知识(4)--转载
第一次尝试(V1.0) 1.理论支持 这里主要要说的关于Socket方面的.主要是一个例子,关于Socket如何建立服务端程序的简单的代码. static void Main(string[] arg ...
- SqlServer存储过程传入Table参数
今天是周日,刚好有空闲时间整理一下这些天工作业务中遇到的问题. 有时候我们有这样一个需求,就是在后台中传过来一个IList<类>的泛型集合数据,该集合是某个类的实例集合体,然后将该集合中的 ...
- .net下载
1,Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下: Content-Disposition: attachment;filename=filename.ext ...