码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】Sort Colors

时间:2014-05-02 17:37:37      阅读:468      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   ext   

LeetCode OJ

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?

 

【最简单:计数排序】

bubuko.com,布布扣
class Solution {
public:
    void sortColors(int A[], int n) {
        int i = 0;
        int j = 0;
        int k = 0;
        for(int p = 0; p < n; p ++)
        {
            if(A[p] == 0)
            {
                i ++;
            }
            else if(A[p] == 1)
            {
                j ++;
            }
            else
                k ++;
        }

        for(int p = 0; p < n; p ++)
        {
            if(p < i)
                A[p] = 0;
            else if(p >= i && p < i + j)
                A[p] = 1;
            else
                A[p] = 2;
        }
    }
};
bubuko.com,布布扣

 bubuko.com,布布扣

【最直观:平移插入】

bubuko.com,布布扣
class Solution {
public:
    void sortColors(int A[], int n) {
        int i = -1;
        int j = -1;
        int k = -1;
        for(int p = 0; p < n; p ++)
        {
            //根据第i个数字,挪动0~i-1串。
            if(A[p] == 0)
            {
                A[++k] = 2;    //2往后挪
                A[++j] = 1;    //1往后挪
                A[++i] = 0;    //0往后挪
            }
            else if(A[p] == 1)
            {
                A[++k] = 2;
                A[++j] = 1;
            }
            else
                A[++k] = 2;
        }

    }
};
bubuko.com,布布扣

 bubuko.com,布布扣

【最暴力:分情况讨论】

bubuko.com,布布扣
class Solution 
{
public:
    void sortColors(int A[], int n) 
    {
        int index0 = 0;        //next position of 0
        int index2 = n-1;    //next position of 2

        while(A[index0] == 0)
            index0 ++;
        while(A[index2] == 2)
            index2 --;

        for(int i = index0; i <= index2; i ++)
        {
            if(A[i] == 0 || A[i] == 2)
            {
                if(A[i] == 2 && A[index0] == 1 && A[index2] == 0)
                {
                    int temp = A[index2];
                    A[index2] = A[i];
                    A[i] = temp;

                    while(A[index2] == 2)
                        index2 --;

                    temp = A[index0];
                    A[index0] = A[i];
                    A[i] = temp;

                    while(A[index0] == 0)
                        index0 ++;

                    if(index0 > i)
                        i = index0;
                }
                else if(A[i] == 2 && A[index0] == 2 && A[index2] == 0)
                {
                    int temp = A[index2];
                    A[index2] = A[i];
                    A[i] = temp;

                    while(A[index2] == 2)
                        index2 --;

                    temp = A[index0];
                    A[index0] = A[i];
                    A[i] = temp;

                    while(A[index0] == 0)
                        index0 ++;

                    if(index0 > i)
                        i = index0;

                    while(A[index2] == 2)
                        index2 --;
                }
                else if(A[i] == 0 && A[index0] == 2 && A[index2] == 1)
                {
                    int temp = A[index0];
                    A[index0] = A[i];
                    A[i] = temp;

                    while(A[index0] == 0)
                        index0 ++;

                    temp = A[index2];
                    A[index2] = A[i];
                    A[i] = temp;

                    while(A[index2] == 2)
                        index2 --;
                }
                else if(A[i] == 0 && A[index0] == 2 && A[index2] == 0)
                {
                    int temp = A[index0];
                    A[index0] = A[i];
                    A[i] = temp;

                    while(A[index0] == 0)
                        index0 ++;

                    temp = A[index2];
                    A[index2] = A[i];
                    A[i] = temp;

                    while(A[index2] == 2)
                        index2 --;

                    while(A[index0] == 0)
                        index0 ++;    

                    if(index0 > i)
                        i = index0;
                }
                else
                {
                    if(A[i] == 0)
                    {
                        int temp = A[index0];
                        A[index0] = A[i];
                        A[i] = temp;

                        while(A[index0] == 0)
                            index0 ++;

                        if(index0 > i)
                            i = index0;
                    }
                    else
                    {// A[i] == 2
                        int temp = A[index2];
                        A[index2] = A[i];
                        A[i] = temp;

                        while(A[index2] == 2)
                            index2 --;
                    }
                }
            }
        }
    }
};
bubuko.com,布布扣

 bubuko.com,布布扣

【LeetCode】Sort Colors,布布扣,bubuko.com

【LeetCode】Sort Colors

标签:style   blog   class   code   java   ext   

原文地址:http://www.cnblogs.com/ganganloveu/p/3703746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!