本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43302343

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.

思路:

(1)题意为给定一个数组,里面含有红白蓝三种颜色(顺序是打乱的),将其调整为红-->白-->蓝的顺序。其中,0,1,2分别对应红,白,蓝。

(2)该题的实质就是一个排序操作。只不过相同的元素比较多,需要确定下来。本文使用的方法比较简单,首先,遍历数组,确定0,1,2的个数;然后,再对数组进行遍历,依次将0,1,2填入数组中。具体操作为,只要0的个数大于零,就将0填入数组,然后个数减1,直到0全部放入数组为止,继续将1和2按照其对应的个数放入数组中。最后,所得的数组中就是以连续的0-->1-->2的顺序分布。

(3)希望本文对你有所帮助。

算法代码实现如下:

	/**
	 *
	 * @author liqq
	 */
	public void sortColors(int[] A) {
		if (A == null || A.length <= 1)
			return;

		int _zeor = 0;
		int _one = 0;
		int _two = 0;

		for (int i = 0; i < A.length; i++) {
			if (A[i] == 0)
				_zeor++;
			if (A[i] == 1)
				_one++;
			if (A[i] == 2)
				_two++;
		}

		for (int i = 0; i < A.length; i++) {
			if (_zeor > 0) {
				A[i] = 0;
				_zeor--;
				continue;
			}
			if (_one > 0) {
				A[i] = 1;
				_one--;
				continue;
			}

			if (_two > 0) {
				A[i] = 2;
				_two--;
			}
		}
	}

Leetcode_75_Sort Colors的更多相关文章

  1. Sort Colors

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

  2. [LeetCode] Sort Colors 颜色排序

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

  3. Leetcode 75. Sort Colors

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

  4. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  6. Sort Colors [LeetCode]

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

  7. 【LeetCode】Sort Colors

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

  8. PAT (Advanced Level) Practise:1027. Colors in Mars

    [题目链接] People in Mars represent the colors in their computers in a similar way as the Earth people. ...

  9. LintCode Sort Colors

    For this problem we need to sort the array into three parts namely with three numbers standing for t ...

随机推荐

  1. 集群技术(二) MySQL集群简介与配置详解

    when?why? 用MySQL集群? 减少数据中心结点压力和大数据量处理(读写分离),采用把MySQL分布,一个或多个application对应一个MySQL数据库.把几个MySQL数据库公用的数据 ...

  2. Programming In Scala笔记-第九章、控制抽象

    本章主要讲解在Scala中如何使用函数值来自定义新的控制结构,并且介绍Curring和By-name参数的概念. 一.减少重复代码 1.重复代码的场景描述 前面定义的函数,将实现某功能的代码封装到一起 ...

  3. Spark Streaming中的操作函数分析

    根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transformations Window Operations J ...

  4. Cassandra Secondary Index 介绍

    摘要 本文主要介绍cassandra中的索引,物化视图,有些知识点需要对cassandra有基本的认识才能理解.比如数据在cassandra节点中如何分布.如果有不明白的地方可以看本专栏之前文章.或者 ...

  5. Android 异步查询框架AsyncQueryHandler的使用

    AsyncQueryHandler简介: 异步的查询操作帮助类,可以处理增删改(ContentProvider提供的数据) 使用场景: 在一般的应用中可以使用ContentProvider去操作数据库 ...

  6. Android Studio中Git的配置及协同开发

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51595096 本文出自:[openXu的博客] 目录: 一 Android Stutio配置 ...

  7. java类加载器——ClassLoader

    Java的设计初衷是主要面向嵌入式领域,对于自定义的一些类,考虑使用依需求加载原则,即在程序使用到时才加载类,节省内存消耗,这时即可通过类加载器来动态加载. 如果你平时只是做web开发,那应该很少会跟 ...

  8. 3-sum问题

    给定一个整数数组,判断能否从中找出3个数a.b.c,使得他们的和为0,如果能,请找出所有满足和为0个3个数对. #define SIZE 10 void judgeAndPut(int* arr, i ...

  9. 剑指offer面试题6 重建二叉树(c)

  10. Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup

    今天在给客户做环境迁移,安装包完成后按惯例打补丁,但在打补丁的时候却报错了,错误如下 最开始怀疑第一个打6是不是不对,毕竟N久没碰2011了忘的差不多了,后来下了个rollup1居然也打不上,根据这个 ...