【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 ...
随机推荐
- [Usaco2005 dec]Layout
题目描述 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的.因为奶牛 ...
- pycharm远程登录mysql
pycharm远程登录mysqlmysql远程登录需要修改配置文件:cd /etc/mysql/mysql.conf.d/sudo vim mysqld.cn修改bing-address=0.0.0. ...
- 兼容ie7到ie11,edge,chrome,firefox的ajax发送接收post数据代码
/* * 生成XMLHttpRequest */ function getxhr() { //获取ajax对象 var xhr = null; try { xhr = new XDomainReque ...
- vboxmanage查询正在运行的vbox虚拟机
系统:linux通用,virtualbox5.0 每次用下面命令启动vm虚拟机时,发现没办法知道它的IP. $ vboxmanage startvm <vmname> --type hea ...
- HTML5 canvas上画文字出现乱码
不是这个的问题, <html> <head> <meta charset="utf-8"/> </head> 是因为从网上复制粘贴下 ...
- IIS——MIME介绍与添加MIME类型
MIME(MultipurposeInternet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会 ...
- Ubuntu 搭建 ***
在Ubuntu下安装ss很简单.只需要依次执行下面3条命令: apt-get update apt-get install python-pip pip install shadowsocks pip ...
- UVALive(LA) 3644 X-Plosives (并查集)
题意: 有一些简单化合物,每个化合物都由两种元素组成的,你是一个装箱工人.从实验员那里按照顺序把一些简单化合物装到车上,但这里存在安全隐患:如果车上存在K个简单化合物,正好包含K种元素,那么他们就会组 ...
- [Math Review] Statistics Basic: Estimation
Two Types of Estimation One of the major applications of statistics is estimating population paramet ...
- MySql笔记之修改MySQL提示符
首先,了解下MYSQL提示符是神马东东 就是每次登陆mysql后出现的提示符 如果我们不喜欢这个提示符呢,那我们就改成我们喜欢的样子. 系统参数提示符 举个栗子 就改成相应的提示符了,那么可否随意改名 ...