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.

颜色排序

思路:

直觉看一定有简单的方法的,可惜我没想出来,就复习一下二分归并排序吧。

    void sortColors(int A[], int n) {
MergeSort(A, , n - );
} void MergeSort(int A[], int l, int r)
{
if(l < r)
{
int mid = (l + r) / ;
MergeSort(A, l, mid);
MergeSort(A, mid + , r);
Merge(A, l, mid, r);
}
}
void Merge(int A[], int l, int mid, int r)
{
int x = mid - l + ;
int y = r - mid;
int * B = new int[x];
int * C = new int[y];
memcpy(B, A + l, x * sizeof(int));
memcpy(C, A + mid + , y * sizeof(int));
int i = , j = , k = l;
while(i < x && j < y)
{
if(B[i] < C[j])
A[k] = B[i++];
else
A[k] = C[j++];
k++;
}
if(i >= x)
memcpy(A + k, C + j, (y - j) * sizeof(int));
else
memcpy(A + k, B + i, (x - i) * sizeof(int)); delete [] B;
delete [] C;
}

看看大神的线性时间、常量空间的方法

①说实话,我一眼望过去看不懂啊。研究了半天,发现 n0是最后一个0个位置, n1是最后一个1的位置, n2是最后一个2的位置

每次新来一个0,需要依次把2、1向后延1位。

就是2先向后多占一个位置,然后1向后多占一个位置,把2最前面的给覆盖,0再多占一个位置,新加的0覆盖了1最前面多出来的。

依此类推。

void sortColors2(int A[], int n) {
int n0 = -, n1 = -, n2 = -;
for (int i = ; i < n; ++i) {
if (A[i] == )
{
A[++n2] = ;
A[++n1] = ;
A[++n0] = ;
}
else if (A[i] == )
{
A[++n2] = ;
A[++n1] = ;
}
else if (A[i] == )
{
A[++n2] = ;
}
}

【leetcode】Sort Colors(middle)☆的更多相关文章

  1. 【LeetCode】Sort Colors 解题报告

    [题目] Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  2. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  3. 【LeetCode】Sort Colors

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

  4. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  5. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  6. 【LeetCode】 sort list 单清单归并

    称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...

  7. 【数组】Sort Colors

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  8. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  9. 【leetcode】Sort List

    Sort List Sort a linked list in O(n log n) time using constant space complexity.   需要采用归并排序对链表进行操作. ...

随机推荐

  1. [设计模式] javascript 之 迭代子模式

    迭代子模式:定义 迭代子模式,又称游标模式,是一种用于对聚集进行顺序访问规则的模式,是一种行为模式:它用于提供对聚集对象的一种统一的访问接口,使客户能够在不了解聚集对象内部结构的情况对聚集对象进行访问 ...

  2. ASP 编码转换(乱码问题解决)

    ASP 编码转换(乱码问题解决) 输出前先调用Conversion函数进行编码转换,可以解决乱码问题. 注,“&参数&”为ASP的连接符,这里面很多是直接调用的数据库表字段,实际使用请 ...

  3. HDU 5592

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5592 线段树的变形,先说思路. 题目中给出了当前节点之前的逆序对数,则p[i]-p[i-1]就是对于p[ ...

  4. ASP.NET发送邮件(QQ发送)

    public void SetEmail()        {            //电子邮件对象            MailMessage mailMessage = new MailMes ...

  5. iOS开发——源代码管理——svn 命令行下常用的几个命令

    1.将文件checkout到本地目录    svn checkout path(path是服务器上的目录)    例如:svn checkout svn://192.168.1.1/pro/domai ...

  6. PHP四种基础算法详解

    许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一个初级phper,虽然很少接触到算法方面的东西 .但是对于冒泡排序,插入排序,选择排序,快速排序四种基本算法,我想还是要 ...

  7. django的中间件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2EAAAGUCAIAAAAzrr3rAAAgAElEQVR4nOy9d5wcx3kmzAXl80m6zy

  8. Ajax基础2

    什么是服务器 网页浏览过程的分析 如何配置自己的服务器程序(AMP) 什么是Ajax 无刷新数据读取 异步,同步 Ajax基础(2) 使用Ajax 基础请求显示txt的文件 字符集编码 缓存,阻止缓存 ...

  9. 一个简单的log

    #pragma once #include <windows.h> #include <process.h> class CLogger { public: static CR ...

  10. Git引用

    原文: http://gitbook.liuhui998.com/7_3.html git中,分支(branch), 远程跟踪分支(remote-tracking branch)以及标签(tag)都是 ...