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函数节流

    浅谈javascript函数节流 什么是函数节流? 函数节流简单的来说就是不想让该函数在很短的时间内连续被调用,比如我们最常见的是窗口缩放的时候,经常会执行一些其他的操作函数,比如发一个ajax请求等 ...

  2. JDBC、JDBCTemplate、MyBatis、Hiberante 比较与分析

    JDBC (Java Data Base Connection,java数据库连接) JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句的Jav ...

  3. js 日期处理,json处理

    模块化js :requirejshttp://www.requirejs.cn/ 好用的日期控件:http://www.bootcss.com/p/bootstrap-datetimepicker/i ...

  4. tc 146 2 BridgeCrossing(n人过桥问题)

    SRM 146 2 1000BridgeCrossing Problem Statement A well-known riddle goes like this: Four people are c ...

  5. 大数据之pig 命令

    1.pig与hive的区别 pig和hive比较类似的,都是类sql的语言,底层都是依赖于hadoop    走的mapreduce任务.    pig和hive的区别就是,想要实现一个业务逻辑的话, ...

  6. JAVA 笔记

    一.Java基础以及面向对象编程1.float类型的数自动转换成double类型时,可能会出现前后不相等的情况,因为有些数不能够用有限的二进制位精确表示.2.右移>>右移,左边空出位以符号 ...

  7. BZOJ1251——序列终结者

    给你一个数列,让你实现区间加上一个值,区间翻转,区间最大值 裸splay,懒标记一发即可 #include <cstdio> #include <cstdlib> #inclu ...

  8. leetcode 173. Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. Laravel 5.1 文档攻略 —— Eloquent:模型关系

    简介 其实大家都知道,数据表之间都是可以关联的,前面讲过了,Eloquent ORM是数据模型操作代替表操作,那么表的关联查询,在Eloquent这里也就是模型间的关联查询,这就是本章的主要内容: E ...

  10. OpenCV中对图像进行二值化的关键函数——cvThreshold()。

    函数功能:采用Canny方法对图像进行边缘检测 函数原型: void cvThreshold( const CvArr* src, CvArr* dst, double threshold, doub ...