Sort Colors 解答
Question
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.
Solution 1 -- Counting Sort
Straight way, time complexity O(n), space cost O(1)
public class Solution {
public void sortColors(int[] nums) {
int[] count = new int[3];
int length = nums.length;
for (int i = 0; i < length; i++)
count[nums[i]]++;
for (int i = 0; i < count[0]; i++)
nums[i] = 0;
for (int i = 0; i < count[1]; i++)
nums[i + count[0]] = 1;
for (int i = 0; i < count[2]; i++)
nums[i + count[0] + count[1]] = 2;
}
}
Solution 2 -- Two Pointers
We can use two pointers here to represent current red position and blue position. redIndex starts from 0, and blueIndex starts from length - 1.
We traverse once from 0 to blueIndex.
Each time we find nums[i] is not 1:
if nums[i] is 0, we move it to redIndex position
if nums[i] is 2, we move it to blueIndex position
Time complexity O(n), space cost O(1)
public class Solution {
public void sortColors(int[] nums) {
int length = nums.length;
int redIndex = 0, blueIndex = length - 1, i = 0;
while (i <= blueIndex) {
// If current color is red, we need to switch it to red position
if (nums[i] == 0) {
// Switch nums[i] with nums[redIndex]
nums[i] = nums[redIndex];
nums[redIndex] = 0;
redIndex++;
i++;
} else if (nums[i] == 2) {
// If current color is blue, we need to switch it to blue position and check switched color
// Switch nums[i] with nums[blueIndex]
nums[i] = nums[blueIndex];
nums[blueIndex] = 2;
blueIndex--;
} else {
i++;
}
}
}
}
Sort Colors 解答的更多相关文章
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 52. Sort Colors && Combinations
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
随机推荐
- SSH2.0编程 ssh协议过程实现
之前为了自己做一套SSH,先自己实现了一套telnet.但经过这么多天的苦逼,发现以前的工作都是徒劳.ssh的协议很繁杂,核心的内容在于密码算法,而且自己很难在网上找到周全的细节讲解与详细的实现,只有 ...
- 【POJ1005】I Think I Need a Houseboat
说是计算几何,其实是一道水题.直接算半圆面积即可. #include <iostream> #include <cstdlib> #include <cstdio> ...
- Shell下通过echo+telnet在远端执行命令
创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令 执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...
- Linux CPU 亲和性
在Linux中,我们知道可以通过nice.renice命令改变进程的执行优先级,优先级高的进程优先执行,从而一定程度上保证重要任务的运行. 除了nice.renice外,可以通过CPU affinit ...
- 01-Java学习笔记
本系列笔记由常彦博整理,请知悉 目 录 一. Java技术基础.................................................................... ...
- XSS第二节,XSS左邻右舍
昨天的文章中引用了OWASP2013年的江湖排名,今天来看一下TOP中XSS的左邻右舍都是谁,先看一下他们的大名,再进一步介绍 [以下主要翻译自https://www.owasp.org/index. ...
- 微信小程序demo豆瓣图书
最近微信小程序被炒得很火热,本人也抱着试一试的态度下载了微信web开发者工具,开发工具比较简洁,功能相对比较少,个性化设置也没有.了解完开发工具之后,顺便看了一下小程序的官方开发文档,大概了解了小程序 ...
- ORACLE数据库常用查询二
ORACLE数据库常用查询 1.查看表空间对应数据文件情况: SQL MB,AUTOEXTENSIBLE FROM DBA_DATA_FILES; TABLESPACE_NAME FILE_NAME ...
- EffectiveC#03--用委托表示回调,用事件定义对外接口
1.回调的场景:我给了儿子一个任务且他可以报告状态来(重复的)打断我.而我在等待他完成任务的每一个部份时不用阻塞我自己的进程.他可以在有重要(或者事件)状态报告时,可以定时的打断我,或者向我询求帮助 ...
- 触摸点为scrollview上的子控件时,scrollview不能滚动(iOS8)
现象:在iOS8上,scrollview上面布局了多行多列的button,滑动scrollview,如果当触摸点是在按钮上,scrollview不能滚动. 例如: 解决方法:设置scrollview的 ...