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. 使用jQuery来检测远程图片文件是否存在

    使用jQuery来检测远程图片文件是否存在 最近为我的憨豆人笑园添加图片功能时,遇到了这个问题,用户可以填写一个远程的图片地址,也可以上传一个本地图片.为了不浪费服务器的资源,我们需要在客户端先对用户 ...

  2. JS帮你计算属相

        背景:一个人出生在2014年的正月初一,他的生肖到底是属蛇还是属马呢?这就要确定那一天才是一年的开始.是春节还是立春?每年的春节是正月初一,但是生肖必须是从立春日开始计算.春节是1912年孙中 ...

  3. ios 计算文字的尺寸

    /** * 计算文字尺寸 * @param text 需要计算尺寸的文字 * @param font 文字的字体 * @param maxSize 文字的最大尺寸 */ - (CGSize)sizeW ...

  4. 跨域资源共享 CORS 详解

    CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...

  5. 给SharePoint页面加入自己定义页脚Custom footer

    给SharePoint页面加入自己定义页脚Custom footer         在公司做站点设计项目时,须要在页面上加入页脚.         非常多人都把页脚忽视了,认为没什么多大用处,事实上 ...

  6. Laravel 安装指南

    http://www.golaravel.com/article/laravel-installation-guide/ http://laravel.com/api/4.1/ http://ding ...

  7. XML读写

    private string fileName = HttpContext.Current.Server.MapPath("~/Student.xml"); protected v ...

  8. ios开发中的基本设计模式

    (一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过和protoc ...

  9. Office 2010

    1.Office Professional Plus 2010: 6QFDX-PYH2G-PPYFD-C7RJM-BBKQ8 BDD3G-XM7FB-BD2HM-YK63V-VQFDK 2.Offic ...

  10. js实现checkbox全选与反选

    <script type="text/javascript" language="javascript"> function checkAll(id ...