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. Web 前端

    全栈的定义是什么? 如果 前端开发/后端开发/部署/运维 都能hold住就算full stack, 我现在都overflow stack了, 需求/架构/开发/项目管理/运维 都做.  单开发这块, ...

  2. R-数据结构

    目录 数据类型(模式) 字符型 数值型 逻辑型 整形 复数型(虚数) 原生型(字节) 数据结构 向量 矩阵 数组 数据框 列表 数据类型 数据结构  向量 用于存储数值型.字符型或逻辑型数据的一维数组 ...

  3. [译]简单得不得了的教程-一步一步用 NODE.JS, EXPRESS, JADE, MONGODB 搭建一个网站

    原文: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 原文的源代码在此 太多的教程教你些一个Hello, World!了, ...

  4. 【AngularJS】—— 13 服务Service

    在AngularJS中有很多的服务,常用的比如$http,$location等等. 本篇文章会介绍一下的内容: 1 $http这种Angular提供的服务的使用 2 如何自定义服务,并总结服务需要注意 ...

  5. javascript 2048游戏

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 调用shell脚本,IP处理

    //调用shell脚本,IP处理 package com.letv.sdns.web.utils; import org.slf4j.Logger; import org.slf4j.LoggerFa ...

  7. html5开发制作,漂亮html5模板欣赏,H5网站建设

    html5是什么? HTML5 是下一代的 HTML(超文本标记语言,网页的组成部分),HTML5是web开发世界的一次重大的改变,能适配pc.手机等各终端,跨平台性能极强,移动互联网是未来的趋势,h ...

  8. 大数据之sqoopCDH 备份

    Sqoop课程笔记 一.概述 1.什么是sqoop? Hadoop的优势在于对数据的存储和处理,相比以前传统的数据库,在处理较较多的数据时,传统数据行业通过提升单机性能以提高处理性能,而且性价比随着性 ...

  9. jquery 表单清空

    $(':input','#myform') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .rem ...

  10. BZOJ2809——[Apio2012]dispatching

    1.题目大意:给一棵树和M值,每个点有两个权值C和L,选x个点,这x个点的C值的和不能超过M,且这x个点如果都在某个子树内 定义满意度为x*这个子树的根的L值 2.分析:这是一道可并堆的题目,我们考虑 ...