排颜色 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. IOS之表视图单元格删除、移动及插入

    1.实现单元格的删除,实现效果如下 - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @&q ...

  2. [转]rpcndr.h和wtypes.h冲突Bug的解决方案

    [转]rpcndr.h和wtypes.h冲突Bug的解决方案 http://blog.csdn.net/tzwh_86/article/details/9495133 rpcndr.h和wtypes. ...

  3. C语言函数返回数组

    #include "stdio.h"/*int* set(int a,int *c){ int *b; b=malloc(sizeof(int)*3); c[0]=a; c[1]= ...

  4. iOS 进阶 第十九天(0423)

    0427 深复制/浅复制 浅复制:是址引用 深复制:是值拷贝 下面是解释,如下图: 运行时机制runtime 1.表象的就是MPMoviePlayer 2.深层的是 clang -rewrite-ob ...

  5. sublime mac快捷键

    ^是control ⌥是option 打开/前往 ⌘T 前往文件 ⌘⌃P 前往项目 ⌘R 前往 method ⌘⇧P 命令提示 ⌃G 前往行 ⌘KB 开关侧栏 ⌃ ` python 控制台 ⌘⇧N 新 ...

  6. 快速排序QuickSort

    前几天实现了直接插入排序.冒泡排序和直接选择排序这三个基础排序.今天看了一下冒泡排序的改进算法,快速排序.单独记录一下,后面还有归并和基数排序等 快速排序 1.选择一个支点默认为数组第一个元素及arr ...

  7. android开发 缩放到指定比例的尺寸

    一种通过matrix矩阵缩放: //使用Bitmap加Matrix来缩放 public static Drawable resizeImage(Bitmap bitmap, int w, int h) ...

  8. codeforces399D

    题目大意:ainta刷一面n*n的二维墙.墙一开始可能有一些块被刷了.他终止刷墙的前提是墙的每一行每一列都至少有一块被刷.他每一次从n*n的墙随机选择一块,如果这一块没有被刷就刷,刷了就无视,刷墙不计 ...

  9. NYOJ-949 哈利波特 AC 分类: NYOJ 2013-12-30 12:57 217人阅读 评论(0) 收藏

    #include<stdio.h> int main(){ long long a,b,c,d,e,f; while(scanf("%lld%lld%lld%lld%lld%ll ...

  10. Centos最小化安装后联网配置

    Centos最小化安装默认不开启网络,只需进行简单配置就可以上网了. 1.  查看/etc/sysconfig/network-scripts/下面的文件,这里会有一个ifcfg-en******(这 ...