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 ...
随机推荐
- 一例完全理解vue 2.0 的slots 和 functional render
https://jsfiddle.net/pronan/mjqpmw0u/ 通过调节plan="bbb"的值, 比如换成plan="children",你会发现 ...
- pyinstaller 工具起步
准备 依赖 pyinstaller下载 语法 核心命令 可选项 实战 md2htmlpy 使用pyinstaller 其他测试 -D选项 --icon选项 遇到错误怎么办 总结 继上次的那个Pytho ...
- 让你的代码量减少3倍!使用kotlin开发Android(一)
让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程 本文同步自博主的私人博客:wing的地方酒馆 写在前面 使用kotlin开发android已经两周多了.得到的好处 ...
- ZooKeeper之(二)数据模型
ZooKeeper 会维护一个具有层次关系的数据结构,它非常类似于一个标准的文件系统: 树形结构的每个节点都被称作为Znode. Zonde通过路径引用,如同Unix中的文件路径.路径必须是绝对的,因 ...
- ExpandableListView的使用
ExpandableListView的使用 效果图 布局 <ExpandableListView android:id="@+id/expandableListView" a ...
- 去除CSDN 博客页广告的历程
第一招 方式1 方式2 第二招 第三招 素材准备 必备知识 代码部分 测试 总结 作为CSDN的忠实用户,我觉得它挺不错的.美中不足的是广告,虽然相比于微博啊,开源中国啊这些博客站点,它的广告已经算是 ...
- Excel init
Sub Test() Dim r As Range Dim a As Integer a = For Each r In Range("b1:b6") If r.Font.Bold ...
- android 集成微博常见问题
我们在做微博集成登录.分享.聊天的时候,肯定会遇到很多的坑,这里总结下常见的问题. 文件不存在 C8998 的解决方法 如图我们走微博授权登录的时候如果OAuth2.0 授权设置回调页面设置和本地的不 ...
- actionbar完全解析(一)
Action Bar是一种新増的导航栏功能,在Android 3.0之后加入到系统的API当中,它标识了用户当前操作界面的位置,并提供了额外的用户动作.界面导航等功能.使用ActionBar的好处是, ...
- iOS开发之使用block块进行数据遍历的方法
看了一篇文章,发现遍历数组.字典中的数据时,除了使用for循环外,还可以使用block块进行操作,瞬间感觉iOS的语言代码确实有点高大上的感觉,下面就简单的介绍一下这个方法. 首先是最基本的运用形式, ...