本文是在学习中的总结,欢迎转载但请注明出处: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. Bootstrap3 排版-页面主体

    Bootstrap 将全局 font-size 设置为 14px,line-height 设置为 1.428.这些属性直接赋予 元素和所有段落元素.另外,<p> (段落)元素还被设置了等于 ...

  2. 转:LINUX/UNIX下的回车换行与WINDOWS下的区别

      今天,我总算搞清楚“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别了.在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 3 ...

  3. 剑指Offer——好未来视频面知识点储备+面后总结

    剑指Offer--好未来视频面知识点储备+面后总结 情景介绍 时间:2016.10.12 13:00- 地点:宿舍 事件:好未来视频面 知识点储备 数据结构 单链表反转 public class Li ...

  4. python学习资料整理

    [1] The Python Tutorial [2] Numpy Quick Start Tutorial [3] Python-OpenCV [4] http://www.learnpython. ...

  5. 非ROOT实现静默安装的一些思考与体会,AIDL获取IPackageManager,反射ServiceManager,系统签名

    非ROOT实现静默安装的一些思考与体会,AIDL获取IPackageManager,反射ServiceManager,系统签名 最近自家的系统要做一个升级服务,里面有三个功能,第一个是系统升级,也就是 ...

  6. 插件开发之360 DroidPlugin源码分析(四)Activity预注册占坑

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52258434 在了解系统的activity,service,broa ...

  7. kindeditor用法简单介绍

    最近做毕业设计用了一个叫做kindeditor的文本编辑工具,相信很多人都用过,这货和fckeditor差不多,个人感觉这个的皮肤更好看,而且对中文的支持更好,没那么容易出现中文乱码问题.下次记录一下 ...

  8. 如何在SpriteBuilder中设置对象的通用属性

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在SpriteBuilder中可以为对象设置自定义类从 ...

  9. 【unix网络编程第三版】阅读笔记(四):TCP客户/服务器实例

    本篇博客主要记录一个完整的TCP客户/服务器实例的编写,以及从这个实例中引发的对僵死进程的处理等问题. 1. TCP客户/服务器功能需求 本实例完成以下功能: (1) 客户从标准输入读入一行文本,并写 ...

  10. 1049. Counting Ones (30)

    题目如下: The task is simple: given any positive integer N, you are supposed to count the total number o ...