【LeetCode】Sort Colors 数组排序
题目:Sort color
<span style="font-size:18px;">/*LeetCode sort colors
题目:输入一个数组。包括0,1,2分别代表红白蓝三种颜色,要求依照0,1,2的顺序,将同类颜色的连续排列
思路:计数排序,是一个遍历两遍的方法:能够先统计每种的数量,之后直接将这一范围内的全部值都赋值为对应的数字就可以
遍历一遍的话能够在遍历的同一时候分别与0和2比較,从头和尾一起交换。1的在中间不用做处理;
*
*/
package javaTrain; public class Train13 {
public void sortColors(int[] A) {
int n = A.length;
int red = 0,blue = n-1; for(int i=0;i < blue+1;){ //由于会从后向前推进所以以blue表示尾部,确保仅仅用遍历一遍
int temp = A[i];
if(temp == 0){
A[i++] = A[red]; //由于red在前。所以交换时它指向的仅仅能是0或1。所以交换后的位置能够向前移
A[red++] = temp;
}
else if(temp == 2){
A[i] = A[blue]; //而blue在后。它指向的之前并没有被比較过有可能有0,1,2所以交换的点不能向前移
A[blue--] = temp;
}
}
}
}
</span>
【LeetCode】Sort Colors 数组排序的更多相关文章
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- [Leetcode] Sort Colors (C++)
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 75.[LeetCode] Sort Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] 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 只有3个类型的排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode Sort Colors (技巧)
题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
随机推荐
- mysql索引记
如果字段是数值型,where 是字符串型,走索引但是,如果字段是字符串型,但是where 是数值型,不走索引
- YYH的营救计划(NOIP模拟赛Round 6)
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!YYH感动的热泪盈眶,开起了门…… YYH的父亲下班回家,街坊邻居说YYH被一群陌生人强行押上了警车!YYH的父 ...
- ScrollLayer
http://www.oschina.net/p/scrolllayer ScrollLayer 编辑/纠错 分享到: 已用 +1 收藏 +12 4月18日 武汉 源创会开始报名,送华为开 ...
- UVA 10940 Throwing cards away II
题意略: 先暴力打表发现规律 N=1 ans=1N=2 ans=2N=3 ans=2N=4 ans=4N=5 ans=2N=6 ans=4N=7 ans=6N=8 ans=8N=9 ans=2N=10 ...
- 非常好!!!Linux源代码阅读——中断【转】
Linux源代码阅读——中断 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/2_int.html 目录 为什么要有中断 中断的作用 中断的处 ...
- Couchbase应用示例(初探)
安装过程:略. 1. 新建Web项目 从NuGet获取并引用: CouchbaseNetClient,添加后引用列表显示为 : Couchbase.NetClient 2. 需要对项目添加引用,这里我 ...
- hdu 5144(三分+物理)
NPY and shot Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- HDU 6299.Balanced Sequence-贪心、前缀和排序 (2018 Multi-University Training Contest 1 1002)
HDU6299.Balanced Sequence 这个题就是将括号处理一下,先把串里能匹配上的先计数去掉,然后统计左半边括号的前缀和以及右半边括号的前缀和,然后结构体排序,然后遍历一遍,贪心策略走一 ...
- 快速幂取模(当数很大时,相乘long long也会超出的解决办法)
当几个数连续乘最后取模时,可以将每个数字先取模,最后再取模,即%对于*具有结合律.但是如果当用来取模的数本身就很大,采取上述方法就不行了.这个时候可以借鉴快速幂取模的方法,来达到大数相乘取模的效果. ...