【Lintcode】颜色分类
来源:互联网 发布:象过河进销存软件 编辑:程序博客网 时间:2024/06/07 04:10
给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红、白、蓝的顺序进行排序。
我们可以使用整数 0,1 和 2 分别代表红,白,蓝。
class Solution {
/*** @param nums: A list of integer which is 0, 1 or 2
* @return: nothing
*/
public void sortColors(int[] nums) {
// write your code here
int a,b,c,n,i;
a=0;b=0;c=0;
n=nums.length;
for(i=0;i<n;i++)
{
if(nums[i]==0)a=a+1;
if(nums[i]==1)b=b+1;
if(nums[i]==2)c=c+1;
}
for(i=0;i<a;i++)
{
nums[i]=0;
}
for(i=a;i<a+b;i++)
{
nums[i]=1;
}
for(i=a+b;i<a+b+c;i++)
{
nums[i]=2;
}
// nums[0]=n;
// nums[1]=a;
// nums[2]=b;
// nums[3]=c;
}
}
0 0
- LintCode-颜色分类
- lintcode-颜色分类-148
- Lintcode: 颜色分类
- 【Lintcode】颜色分类
- LintCode:颜色分类
- lintcode--颜色分类
- 颜色分类-LintCode
- [LintCode] 颜色分类 Sort Colors
- [Lintcode]Sort Colors颜色分类
- lintcode刷题——颜色分类
- 颜色分类
- 颜色分类
- 颜色分类
- 颜色分类
- 颜色分类
- 颜色分类
- LintCode : 排颜色 II
- LintCode:排颜色 II
- 当file_get_contents或者simplexml_load_file的时候乱码
- html中标签传参的奇葩问题
- python的with语句理解
- 开源数据绑定框架android-databinding与google databinding对比
- 浏览器与服务器的消息通信
- 【Lintcode】颜色分类
- 58.respondsToSelector和conformsToProtocol方法的应用
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- 机房收费系统遇到的问题(四) 几点一定要注意的
- [BZOJ3531][SDOI2014]旅行(链剖+线段树动态开点)
- 20160321servlet学习笔记获取web.xml配置参数
- 【GDOI三校联考】Pow
- HashMap循环遍历方式及其性能对比
- mac如何共享windows的打印机