排颜色 II

给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序。

样例

给出colors=[3, 2, 2, 1, 4]k=4, 你的代码应该在原地操作使得数组变成[1, 2, 2, 3, 4]

解题

直接快排

class Solution {
/**
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] colors, int k) {
// write your code here
sort(colors,0,colors.length - 1);
}
public void sort(int[] A,int low,int high){
if(low >= high)
return ;
int i = low;
int j = high;
int tmp = A[low];
while(i<j){
while(i<j && A[j]>tmp) j--;
if(i<j){
A[i] = A[j];
i++;
}
while(i<j && A[i]<= tmp) i++;
if(i<j){
A[j] = A[i];
j--;
}
}
A[i] = tmp;
sort(A,low,i-1);
sort(A,i+1,high);
}
}

Java Code

标记法,先统计各个颜色的次数,然后根据数组更改次数

class Solution {
/**
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] colors, int k) {
// write your code here
int[] flag = new int[k+1];
for(int i = 0;i<colors.length;i++){
flag[colors[i]]++;
}
int c = 1;
for(int i = 0;i<colors.length;i++){
while(flag[c]==0){// 颜色可能为0 的比较多
c++;
}
colors[i] = c;
flag[c]--;
}
} }

只有三种颜色的排序 的时候,但是当有多个的时候判断情况太多。

九章看到两个指针的方法

class Solution {
/**
* @param colors: A list of integer
* @param k: An integer
* @return: nothing
*/
public void sortColors2(int[] colors, int k) {
int count = 0;
int start = 0;
int end = colors.length-1;
while (count < k) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE; for (int i = start; i <= end; i++) {
min = Math.min(min, colors[i]);
max = Math.max(max, colors[i]);
}
int left = start;
int right = end;
int cur = left;
while(cur <= right) {
if (colors[cur] == min) {
swap(left, cur, colors);
cur++;
left++;
} else if (colors[cur] > min && colors[cur] < max) {
cur++;
} else {
int tmp = colors[cur];
swap(cur, right, colors);
right--;
}
}
count += 2;
start = left;
end = right;
}
} void swap(int left, int right, int[] colors) {
int tmp = colors[left];
colors[left] = colors[right];
colors[right] = tmp;
} }

理解不透,脑子太笨。

lintcode:排颜色 II的更多相关文章

  1. lintcode-143-排颜色 II

    143-排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 注意事项 You are not ...

  2. 排颜色问题——数组 leetcode lintcode

    问题描写叙述: 给一个数组,而且数组里面元素的值仅仅可能是0,1,2,然后如今把这个数组排序. 第二种表述: 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换随意两个球,使得从左至右, ...

  3. Lintcode: Sort Colors II 解题报告

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

  4. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

  5. [LintCode] Sort Integers II 整数排序之二

    Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...

  6. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  7. [LintCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  8. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  9. Lintcode: Sort Colors II

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

随机推荐

  1. MVC4.0 WebApi如何设置api支持namespace

    1.自定义HttpControllerSelector /// <summary> /// 设置api支持namespace /// </summary> public cla ...

  2. [转]AngularJS: 使用Scope时的6个陷阱

    在使用AngularJS中的scope时,会有6个主要陷阱.如果你理解AngularJS背后的概念的话,这6个点其实非常的简单.但是在具体讲述这6个陷阱之前我们先要讲两个其它的概念. 概念1: 双向数 ...

  3. VC++程序中加入自定义声音(PlaySound函数用法)

    VC++编程中,我们可以为自己的程序加入音乐,比如当我们按下一个按钮时或者启动程序时,播放一小段音乐. 该功能用到函数: BOOL PlaySound(LPCSTR pszSound, HMODULE ...

  4. 20145129 《Java程序设计》第5周学习总结

    20145129 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承架构 使用try.catch Java中所有错误都会被打包为对象,可以尝试(try)捕捉(catch)代表 ...

  5. “来用”alpha版使用说明书

    1引言 1 .1编写目的 针对我们发布的alpha版本做出安装和使用说明,使参与内测的人员及用户了解软件的使用方法和相关内容. 1 .2参考资料 <c#程序设计基础>,赵敏主编,2011, ...

  6. android开发,关于android app实现静默安装自己(系统签名)

    产品需求,木有办法.android系统是跟厂商定制的,保证系统开机就运行我们的app,并且实现自己静默安装,完全自动化,无需人工操作. 网上有很多办法, 1.要么要通过android 源码拿到密钥文件 ...

  7. 【Construct Binary Tree from Inorder and Postorder Traversal】cpp

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  8. myBatis自动生成相关代码文件配置(Maven)

    pom.xml文件添加配置 <build> <finalName>generator</finalName> <plugins> <!-- mav ...

  9. 如何在Android模拟器上安装apk文件

    1.运行SDK Manager,选择模拟器,并运行模拟器 SDK Manager应用 2.将需要安装的apk文件复制到platform-tools目录下(默认在:D:\tools\android\ad ...

  10. 06.Hibernate实体类生命周期

        前言:Session接口是Hibernate向应用程序提供的操作数据库的主要接口,它提供了基本的增删查改方法,而且Session具有一个缓存它是Hibernate的一级缓存.站在持久化层的角度 ...