lintcode:排颜色 II
排颜色 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的更多相关文章
- lintcode-143-排颜色 II
143-排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 注意事项 You are not ...
- 排颜色问题——数组 leetcode lintcode
问题描写叙述: 给一个数组,而且数组里面元素的值仅仅可能是0,1,2,然后如今把这个数组排序. 第二种表述: 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换随意两个球,使得从左至右, ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- [LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [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 ...
- [LintCode] House Robber II 打家劫舍之二
After robbing those houses on that street, the thief has found himself a new place for his thievery ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
随机推荐
- iOS 数据库持久化
Java代码 -(void) addObserver{ //当程序进入后台时执行操作 UIApplication *app = [UIApplication sharedApplication]; [ ...
- 上传图片的回调函数,callback作为一个函数针对回调函数
Tool.ImageUpload = function (selector, callback) { /// <summary>图片上传</summary> /// <p ...
- meta 页面元信息定义
禁止页面缓存 1.name属性 name属性主要用于描述网页,与之对应的属性值为content,content中的内容主要是便于搜索引擎机器人查找信息和分类信息用的. meta标签的name属性语法格 ...
- 字符串流sstream[part2/使用同一个字符串流反复读写数据]
stringstream构造函数会特别消耗内存,似乎不打算主动释放内存(或许是为了提高效率),如果你要在程序中使用同一个流反复读写大量数据,将会造成大量的内部消耗,因此建议: 1:调用clear ...
- 针对《来用》的NABC分析
项目名:<来用> 特点:拥有以往win7在内的众多小游戏 NABC分析 N(need需求): 之所以有这个想法是因为,在WIN7,XP系统中往往有很多众所周知的小游戏(比如扫雷),但是在w ...
- android webview开启html5支持
最近做的一个小项目需要用到webview.虽然只是一个简单的网页,但是由于以前用的都只是显示本地文件,没有显示网页文件.现在需要显示网页文件,发现许多网站的webapp做的挺不错的,无论是显示还是用户 ...
- 前端之JavaScript第二天学习(5)-JavaScript-语句
JavaScript 语句 JavaScript 语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. 下面的 JavaScript 语句向 id="demo" 的 HTML ...
- mp3 切割
开源的东东很不错,摘了一段好文: 常在听mp3或其他格式音乐的朋友,有时会有特别喜欢的片段,例如副歌的部份会想拿来做手机的铃声.这时候就需要一些处理音效的软体,例如之前提过的 Audacity.其实还 ...
- Spring MVC 学习笔记 data binding
最近在实验Spring的时候遇到了一个问题: SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with ...
- 使用Fiddler
右键一个Result,点击Inspect in new Window(Shift+Enter)