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 ...
随机推荐
- 福建省队集训被虐记——DAY1
今天算是省冬的第一天--早上柯黑出题,说是"信心欢乐赛",其实是"使你失去信心.不再欢乐的比赛" 顺便orz一下来看这篇文章的各路神犇--求轻虐 水题 (py. ...
- C 本地文件夸网文件Cp操作
1,linux平台C简单实现本地文件cp 码子及运行效果测试
- WPF ICommand 用法
基础类,继承与ICommand接口 using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- 01串(dp)
01串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有“11”子串的这种长度的01串共有多少个, ...
- python实现二叉树和它的七种遍历
介绍: 树是数据结构中很重要的一种,基本的用途是用来提高查找效率,对于要反复查找的情况效果更佳,如二叉排序树.FP-树. 另外能够用来提高编码效率,如哈弗曼树. 代码: 用python实现树的构造和几 ...
- NTP配置实践
前言 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.不管是平时使用的私人计算机还是在工作中搭建的服务器集群.时间的统一性和准确性是十分 ...
- LDA-线性判别分析(一)
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- emacs window版环境配置(设置默认的.emacs文件,指向自定义.emacs达到自定义home的目的)
1.下载解压包 下载地址 ,下载之后我是直接解压到E:\emacs中的,E:\emacs中就有bin,libexec…等文件; 2.点击bin中的addpm.exe文件进行安装emacs; 3.就会 ...
- 函数nvl,nvl2,nullif,coalesce
NVL: Converts a null value to an actual valueNVL2:If expr1 is not null, NVL2 returns expr2. If expr1 ...
- spring mvc + mybatis + spring aop声明式事务管理没有作用
在最近的一个项目中,采用springMVC.mybatis,发现一个很恼人的问题:事务管理不起作用!!网上查阅了大量的资料,尝试了各种解决办法,亦未能解决问题! spring版本:3.0.5 myba ...