[LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
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.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
class Solution {
public:
void sortColors(int A[], int n) {
if(n <= ) return;
int start = -, end = n;
int p = ;
while(p < n){
if(A[p] == ){
if(p > start) swap(A, ++start, p);
else ++p;
}else if(A[p] == ){
if(p < end) swap(A, p, --end);
else ++p;
}else ++p;
}
}
private:
void swap(int A[], int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
};
网上还有更加通用和直观的解法
public void sortColors(int[] A) {
int i=-, j=-, k=-;
for(int p = ; p < A.length; p++)
{
if(A[p] == )
{
A[++k]=;
A[++j]=;
A[++i]=;
}
else if (A[p] == )
{
A[++k]=;
A[++j]=;
}
else if (A[p] == )
{
A[++k]=;
}
}
}
稍微简化一点:
public void sortColors(int[] A) {
int i=-, j=-;
for(int p = ; p < A.length; p++) {
int v = A[p];
A[p] = ;
if (v == ) {
A[++j] = ;
A[++i] = ;
}
else if (v == ) {
A[++j] = ;
}
}
}
[LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法的更多相关文章
- 统计数组中各个元素出现的次数,元素取值范围为:1到N
问题描述: * 给定一个整数数组a,长度为N,元素取值范围为[1,N]. * 统计各个元素出现的次数,要求时间复杂度为O(N),空间复杂度为O(1). * 可以改变原来数组结构. 思路: * 从第 ...
- jQuery 表单元素取值与赋值方法总结
一.普通文本框的赋值与取值 1.1.1赋值 <h2>jQuery 表单元素取值与赋值方法总结</h2> <input type="text" clas ...
- jQuery对html元素取值与赋值
以下总结了常用的jQuery选择器对html元素取值与赋值 Textbox: var str = $('#txt').val(); $('#txt').val("Set Lbl Value ...
- python迭代器-迭代器取值-for循环-生成器-yield-生成器表达式-常用内置方法-面向过程编程-05
迭代器 迭代器 迭代: # 更新换代(其实也是重复)的过程,每一次的迭代都必须基于上一次的结果(上一次与这一次之间必须是有关系的) 迭代器: # 迭代取值的工具 为什么用迭代器: # 迭代器提供了一种 ...
- 寻找链表倒数第k个元素,只遍历一遍(编程之美)
class LNode { public LNode next; public int data; } /*找出倒数第k个元素,只遍历一遍*/ class Kk { private static LN ...
- [LeetCode] Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode]Sort Colors @ Python
原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...
- 假设数组a有n个元素,元素取值范围是1~n,如何判定数组是否存在重复元素
方法一:位图法,原理是首先申请一个长度为n且均为’0’组成的字符串,字符串的下标即为数组a[]中的元素,然后从头开始遍历数组a[N],取每个数组元素的值,将其对应的字符串中的对应位置置1,如果已经置过 ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
随机推荐
- C struct中的位域 bitfield
C struct中的位域 bitfield 结构体的成员可以限制其位域,每个成员可以使用用比字节还小的取值范围,下面的结构体s1中,四个成员每个成员都是2bit的值(0~3),整个结构体占据的空间依然 ...
- http://www.cnblogs.com/120626fj/p/7545958.html
1.本周PSP 2.本周进度条: 代码行,博文字数,用到的知识点 3.累计进度图 3.1累计代码折线图 3.2累计博文字数折线图 4.本周PSP饼状图
- Why is setTimeout(fn, 0) sometimes useful?
http://stackoverflow.com/questions/779379/why-is-settimeoutfn-0-sometimes-useful jquery validation s ...
- 【week4】技术随笔psp
本周psp
- PHP获取网页内容的几种方法
方法1: 用file_get_contents以get方式获取内容 <?php $url='http://www.domain.com/?para=123'; $html= file_get_c ...
- 基于实现Controller接口的简单Spring工程
这个Spring工程的特点是:实现了Controller接口(这样就可以在url中传参数?,待调查) 一下为代码,可运行. 1,web.xml <servlet> <servlet- ...
- C运行时库
原文地址:http://blog.csdn.net/wqvbjhc/article/details/6612099 在开发window程序是经常会遇到编译好好的程序拿到另一台机器上面无法运行的情况,这 ...
- go的IO函数,整理下最基本的IO处理函数,工欲善其事必先利其器
bufio.NewScanner()函数是一行一行地读,但是对/proc/函数,这里不是个好方法,最好是把所有的数据一次读完,然后再去读,有没有这样读的接口呢?把所有数据都读入到内存中然后再通过通过搜 ...
- SpringBoot2.0(三) 文件上传
SpringBoot中发起文件上传示例: /** * 文件上传 * @param multipartFile * @param path * @return */ @RequestMapping(va ...
- Bootstrap 折叠(collapse) 初见
以下代码来自bootstrap中文网 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta c ...