zoukankan      html  css  js  c++  java
  • 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.

    click to show follow up.

    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?

    Method I

    如提示,最简单的就是扫描两遍,第一遍计数,第二遍填数。

    32ms

     1 class Solution {
     2 public:
     3     void sortColors(int A[], int n) {
     4         int red = 0, white = 0, blue = 0; 
     5         for (int i = 0; i < n; ++i) {
     6             switch(A[i]) {
     7                 case 0: red++; break;
     8                 case 1: white++; break;
     9                 case 2: blue++; break;
    10             }
    11         }
    12         
    13         for (int i = 0; i < red; ++i) A[i] = 0;
    14         for (int i = 0; i < white; ++i) A[red + i] = 1;
    15         for (int i = 0; i < blue; ++i) A[red + white + i] = 2;
    16     }
    17 };

    Method II

    一遍的没想清楚,看完网上的思路再重写了一遍。我的理解是这样:

    双指针,red 记录已经放在正确位置的0的个数,blue记录已经放在正确位置的2的个数。同时用i从头扫描到blue。

    1. 当碰到0时,放到A[red]这个位置上,red需要累加,i也需要累加。red累加很好理解,i累加是因为red必然<=i, 当A[i]=0时,和A[red]交换之后,要想在A[red+1...i]之间再找到0已经不可能了,因为0值都被交换到red前面了,所以要想再找到0值只能i++。

    2. 当i到达blue的位置的时候,因为A[blue+1...n-1]都是2,所以已经不用再继续了。可以退出。

    3. 注意blue这个位置还没有填充2,所以i是可以,也必须走到blue的。

    8ms

    class Solution {
    public:
        void sortColors(int A[], int n) {
            int i = 0;
            int red = 0, blue = n - 1;
            while (i <= blue) {
                if (A[i] == 0) {
                    swap(A[i], A[red]);
                    red++;
                    i++;
                } else if (A[i] == 2) {
                    swap(A[i], A[blue]);
                    blue--;
                } else {
                    i++;
                }
            }
        }
    };

     Method III

    另一种单遍扫描的算法。one、two、zero分别表示1、2、0的当前更新位置。注意这个位置是小于等于当前位置p的。当出现0时,1和2的位置需要移动一位。当出现1时,只有2会受影响。当出现2时,其他都不受影响。

     1 class Solution {
     2 public:
     3     void sortColors(int A[], int n) {
     4         int one = 0, two = 0, zero = 0; 
     5         
     6         for (int i = 0; i < n; ++i) {
     7             if (A[i] == 2) {
     8                 A[two++] = 2;
     9             } else if (A[i] == 1) {
    10                 A[two++] = 2;
    11                 A[one++] = 1;
    12             } else {
    13                 A[two++] = 2;
    14                 A[one++] = 1;
    15                 A[zero++] = 0;
    16             }
    17         }
    18     }
    19 };
  • 相关阅读:
    Javascript学习总结
    MVC和MVVM
    各大搜索引擎网址收录入口地址
    微软称IE9将更加出色 对手谷歌也能从中受益(图文)
    公安部:身份证丢失无需挂失 被冒用不担责 冒用身份证犯罪
    微软发布Mac 8版Messenger 支持视频会议(图)
    如何删除XP系统的NETWARE,改变登陆界面 火急!!XP系统登录界面由于netware造成的不能更改
    使用WebBrowser自动登录阿里妈妈网站
    微软发布IE9开发者预览版 不支持XP系统(图)
    ASP 0113 的错误的终极解决办法(三种)
  • 原文地址:https://www.cnblogs.com/linyx/p/3709940.html
Copyright © 2011-2022 走看看