Given an array with n objects colored redwhite 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 01, and 2 to represent the color red, white, and blue respectively.

You should do it in-place (sort numbers in the original array).

Analysis:

Use two pointers p, q, p = 0 and q = A.length - 1. If 0 is found, swap 0 with the number pointed by p, then p++. If 2 is found, swap 2 with the number pointed by q, then q--.

 public class Solution {
public void sortColors(int[] A) {
if (A == null || A.length == ) return;
int leftPointer = , rightPointer = A.length - , current = ; while (current <= rightPointer) {
if (A[current] == ) {
swap(A, current, leftPointer);
leftPointer++;
current++; // we can garantee the left values are valid
} else if (A[current] == ) {
swap(A, current, rightPointer);
rightPointer--;
} else {
current++;
}
}
}
public void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}

Sort Colors

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.

Example

Given colors=[3, 2, 2, 1, 4]k=4, your code should sort colors in-place to[1, 2, 2, 3, 4].

分析: http://www.cnblogs.com/yuzhangcmu/p/4177326.html

inplace,并且O(N)时间复杂度的算法。

我们可以使用类似桶排序的思想,对所有的数进行计数。

1. 从左扫描到右边,遇到一个数字,先找到对应的bucket.比如

3 2 2 1 4

第一个3对应的bucket是index = 2 (bucket从0开始计算)

2. Bucket 如果有数字,则把这个数字移动到i的position(就是存放起来),然后把bucket记为-1(表示该位置是一个计数器,计1)。

3. Bucket 存的是负数,表示这个bucket已经是计数器,直接减1. 并把color[i] 设置为0 (表示此处已经计算过)

4. Bucket 存的是0,与3一样处理,将bucket设置为-1, 并把color[i] 设置为0 (表示此处已经计算过)

5. 回到position i,再判断此处是否为0(只要不是为0,就一直重复2-4的步骤)。

6.完成1-5的步骤后,从尾部到头部将数组置结果。(从尾至头是为了避免开头的计数器被覆盖)

例子(按以上步骤运算):

3 2 2 1 4

2 2 -1 1 4

2 -1 -1 1 4

0 -2 -1 1 4

-1 -2 -1 0 4

-1 -2 -1 -1 0

 class Solution {
/*
public void sortColors2(int[] colors, int k) {
if (colors == null || colors.length == 0 || k <= 1) return; int len = colors.length;
for (int i = 0; i < len; i++) {
// Means need to deal with A[i]
while (colors[i] > 0) {
int num = colors[i];
if (colors[num - 1] > 0) {
// 1. There is a number in the bucket,
// Store the number in the bucket in position i;
colors[i] = colors[num - 1];
colors[num - 1] = -1;
} else {
// 2. Bucket is using or the bucket is empty.
colors[num - 1]--;
// delete the A[i];
colors[i] = 0;
}
}
}
int pointer = colors.length - 1;
for (int i = colors.length - 1; i >= 0; i--) {
int count = colors[i];
if (count == 0) continue; while (count < 0) {
colors[pointer--] = i + 1;
count++;
}
}
}
*/
public void sortColors2(int[] colors, int k) {
if (colors == null || colors.length == || k <= ) return;
int[] count = new int[k];
int len = colors.length;
for (int i = ; i < len; i++) {
count[colors[i] - ]++;
}
int index = ;
for (int i = ; i < count.length; i++) {
int p = ;
while (p < count[i]) {
colors[index] = i + ;
index++;
p++;
}
}
}
}

Sort Colors I & II的更多相关文章

  1. Lintcode: Sort Colors II 解题报告

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

  2. Lintcode: Sort Colors II

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

  3. LeetCode: Sort Colors 解题报告

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

  4. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  5. 【LeetCode】Sort Colors

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

  6. 52. Sort Colors && Combinations

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

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

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

  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解题报告—— 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 ...

随机推荐

  1. 【AGC010F】Tree Game

    Description 有一棵\(n\)个节点的树(\(n \le 3000\)),第\(i\)条边连接\(a_i,b_i\),每个节点\(i\)上有\(A_i\)个石子,高桥君和青木君将在树上玩游戏 ...

  2. 【BZOJ2731】三角形覆盖问题

    想象一条平行于\(y\)轴的扫描线,从低往高扫描.如何确定关键高度才能使每两个关键高度之间分割出的图形易于计算呢? 关键高度有:三角形底边高度.三角形上顶点高度.三角形交点的高度. ​ 如此分割,我们 ...

  3. 使用IPMI控制/监控Linux服务器

    1       IPMI简述 IPMI提供了很多丰富功能,我使用的功能,说得大白话一点,就是: 1.获取本设备的硬件信息:包括CPU和主板的温度.电压.风扇转速. 2.在设备A上,通过命令,控制远程设 ...

  4. D. Huge Strings Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    http://codeforces.com/contest/868/problem/D 优化:两个串合并 原有状态+ 第一个串的尾部&第二个串的头部的状态 串变为第一个串的头部&第二个 ...

  5. Java入门系列:处理Json格式数据

    本节主要讲解: 1)json格式数据处理方法 2)第三方工具包的使用方法 3)java集合数据类型 [项目任务] 编写一个程序,显示未来的天气信息. [知识点解析] 为了方便后面代码的分析,先需要掌握 ...

  6. https 协议信息查看

    https://www.ssllabs.com/ssltest/」—————————

  7. 彻底搞懂 SQLAlchemy中的 backref

    教程源码截取: class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True) name = Colum ...

  8. jQuery常用知识总结

    jQuery常用知识总结 简介 选择器 属性操作 jQuery() each event事件 jQuery扩展 一.简介 What is jQuery jQuery is fast small and ...

  9. vue项目post请求405报错解决办法。

    步骤一: 确定ajax语法没有错误. 步骤二: 与后台对接确认请求是否打到nginx上? 步骤三: 检查nginx是否配置了事件转发,比如我们的接口是在,当前地址的8100端口上,并且接口地址上有v1 ...

  10. T48566 【zzy】yyy点餐

    T48566 [zzy]yyy点餐 题目描述 yyy去麦肯士吃垃圾食品. 麦肯士有n种单点餐品(汉堡薯条鸡翅之类的).每次选择一种或者以上的餐点,且每种餐点不多于一个的话,可以认为是购买套餐.购买一个 ...