【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 ...
随机推荐
- hihoCoder挑战赛34 B题(快速求第k轮冒泡排序的结果)
官方题解:https://media.hihocoder.com/contests/challenge34/tutorials-previewed.pdf 题目链接:http://hihocoder. ...
- 限制MYSQL从服务器为只读状态
修改全局变量的方法有两种,第一种是修改配置文件,第二种是SQL语句设置全局变量的值.(可以参考:http://www.cnblogs.com/qlqwjy/p/8046592.html) 0.简介: ...
- 计算器的改良(纯字符串)o1
原题传送门 这题比较水,就是细节..多了点. 首先字符串要处理好(废话..) 基础不行的话要多去看看书.. 然后捏,这题主要就是几个判断: 当我们读字符,如果读到运算符号,那么就要停下来,把之前的常数 ...
- OpenGL “太阳、地球和月亮”天体运动动画 例子
http://oulehui.blog.163.com/blog/static/7961469820119186616743/ OpenGL “太阳.地球和月亮”天体运动动画 例子 2011-10-1 ...
- VLC for iOS 2.3.0
http://www.cocoachina.com/bbs/read.php?tid=231898 VLC for iOS 2.3.0 本帖属于CocoaChina会员发表,转帖请写明来源 ...
- UBI 文件系统移植 sys 设备信息【转】
转自:http://blog.chinaunix.net/uid-25304914-id-3058647.html cat /sys/class/misc/ubi_ctrl/dev --------- ...
- CPU负载监控
#!/usr/bin/python #-*- encoding: utf-8 -*- import os import time while True: loadavg=os.popen(" ...
- python delete数据
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/24 0:27 # @Author : lijunjiang # @Fil ...
- django使用mysql的设置与迁移
1.创建数据库 create database django_lianxi charset=utf8; 2.django项目文件夹的setting.py设置 Django项目默认 sqlite3 数据 ...
- 【转载】无需图片,使用CSS3实现圆角按钮
原文地址:http://www.open-open.com/home/space-37924-do-blog-id-5789.html 首先来看看效果: 事例HTML代码: <a href=&q ...