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 解答的更多相关文章

  1. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  2. 【LeetCode】Sort Colors

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  3. 52. Sort Colors && Combinations

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  4. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  5. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  6. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  7. Sort Colors I & II

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. 【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 ...

  9. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

随机推荐

  1. Increasing/ Decreasing Stack

    对于此类问题: 对于元素nums[i],找出往左/右走第一个比它小/大的数字 我们常常用递增栈/递减栈实现. 递增栈实现第一个比它小 递减栈实现第一个比它大 Example: 2 1 5 6 2 3 ...

  2. 用特殊字体来实现矢量ICON

    用特殊字体来实现矢量ICON tips:其实每个icon都是一个unicode字符,所以,可以通过改变font-size实现icon的矢量放大:可以通过改变color实现多色系.

  3. Linux用户管理(笔记)

    用户:UID, /etc/passwd组:GID, /etc/group 影子口令:用户:/etc/shadow组:/etc/gshadow 用户类别:管理员:0普通用户: 1-65535    系统 ...

  4. [Python学习笔记][第四章Python字符串]

    2016/1/28学习内容 第四章 Python字符串与正则表达式之字符串 编码规则 UTF-8 以1个字节表示英语字符(兼容ASCII),以3个字节表示中文及其他语言,UTF-8对全世界所有国家需要 ...

  5. MySQL 触发器的定义

    -- Insert DELIMITER $$ USE `testdatabase`$$ DROP TRIGGER /*!50032 IF EXISTS */ `Trigger_XXX_INSERT`$ ...

  6. jQuery中click()与trigger方法的区别

    click()可以执行单击事件,但是不可传参. $("button").click(function(){ alert("hello."); }); trigg ...

  7. C# using垃圾回收详解

    简介 定义一个范围,将在此范围之外释放一个或多个对象. 语法 using (Font font1 = new Font("Arial", 10.0f)) { } C# 语言参考 主 ...

  8. Struts2之Action基础与配置

    Action基础 Action是什么 在Struts2中,一个Action类代表一次请求或调用,每个请求的动作都对应于一个相应的Action类,一个Action类是一个独立的工作单元.也就是,用户的每 ...

  9. c_str()函数

    #include <string.h> const char *c_str(); 返回字符串地址,是一个c函数,返回类型const char*c_str()函数返回一个指向正规C字符串的指 ...

  10. [转]Python存取XML方法简介

    转自:http://www.cnblogs.com/salomon/archive/2012/05/28/2518648.html 目前而言,Python 3.2存取XML有以下四种方法: 1.Exp ...