Leetcode_75_Sort Colors
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- Sort Colors [LeetCode]
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 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. ...
- LintCode Sort Colors
For this problem we need to sort the array into three parts namely with three numbers standing for t ...
随机推荐
- Linux 高性能服务器编程——高级I/O函数
重定向dup和dup2函数 #include <unistd.h> int dup(int file_descriptor); int dup2(int file_descriptor_o ...
- FORM中读取图片
1.创建ITEM 重要属性如下 item属性:图像 大小样式:调整 数据库项:否 2.读取触发器 在block级别,创建trigger READ_IMAGE_FILE('D:\'||:XX_EMOLY ...
- socket系列之socket服务端与客户端如何通信
上面已经分别介绍了ServerSocket跟Socket的工作步骤,并且从应用层往系统底层剖析其运作原理,我们清楚了他们各自的一块,现在我们将把他们结合起来,看看他们是如何通信的,并详细讨论一下他们之 ...
- Oracle AP Invoice APIs
These APIs are handful when you want to do Insert, Update or Delete programmatically for some bus ...
- 如何禁止App在后台运行以及如何保存和恢复App的状态
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果禁止App在后台运行 iOS上的App类似于Windows ...
- Centos7安装JStorm2.1.1
系统环境 Centos7 外网ip 182.254.145.66 内网ip 10.105.23.114 安装位置 /usr/local/jstorm-2.1.1 安装zookeeper 参见 htt ...
- 同步图计算:GraphLite的安装和使用
http://blog.csdn.net/pipisorry/article/details/51350908 export HADOOP_HOME=/usr/local/hadoop-2.6.4ex ...
- Android实现横屏以及全屏的小技巧
分享两个安卓的实用小技巧,那就是横屏和全屏的实现. 首先是横屏的实现 首先是在清单文件中实现 <activity android:name=".MainActivity" a ...
- iOS中 项目开发易错知识点总结 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 点击return取消textView 的响应者 - (BOOL)textFieldShouldReturn:(UI ...
- UI设计--大象无形
UI设计,大象无形 UI设计,如同优雅的艺术品一样,优秀的UI设计也可以大象无形,大象无形的意思是有意化无意.大象化无形!就是不要显刻意,不要过分的主张,要兼容百态.无形态无框架才能容纳一切形体! ...