Leetcode---75. Sort Colors

Source: Internet
Author: User

Topic Link: Sort Colors

Given an array with n objects colored red, white or blue, sort them so, objects of the same color is adjacent, with T He colors in the order red, white and blue.

Here, we'll use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:

You is not a 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 a one-pass algorithm using only constant space?

The requirement for this problem is to sort the arrays with only 0, 1, and 2.

1. Counting sort

Direct thinking, counting sort. Iterate over the array first, counting the 0,1 and the number of 2. The 1 variable group is then iterated, and the array is re-assigned according to the number of previous statistics.

However, you need to iterate through the array two times, the following two methods only need to iterate through the array.

Time complexity: O (N)

Space complexity: O (m)

1 class Solution2 {3  Public:4     void sortcolors(int A[], int N)5     {6         int CNT[3] = {0, 0, 0};7          for(int I = 0; I < N; ++ I)8             ++ CNT[A[I]];9         Ten          for(int I = 0, J = 0; I < 3; ++ I) One              while(CNT[I] --) A                 A[J ++] = I; -     } - };
2. Left and right hands

Because there are only 3 kinds of numbers to sort, so just put 0 on the left, 2 on the right, and the remaining 1 in the middle. Therefore, 2 pointers are used to mark the right position of the left 0 and to the left of the right 1, and the elements are exchanged continuously.

Time complexity: O (N)

Space complexity: O (1)

1 class Solution2 {3  Public:4     void sortcolors(int A[], int N)5     {6         int Zero = 0,  Both = N - 1;7          for(int I = 0; I <=  Both; ++ I)8         {9              while(A[I] == 2 && I <  Both)Ten                 Swap(A[I], A[ Both --]); One              while(A[I] == 0 && I > Zero) A                 Swap(A[I], A[Zero ++]); -         } -     } the };
3. Using the Index

Use 3 variables, zero, one, and two to mark the end index position of the ordered 0,1 and 2 respectively. When reading to 0, 3 variables are shifted one after the other, the same is set, and when reading 1, one and two move back a bit and set, and when reading 2, only two moves back and resets.

Time complexity: O (N)

Space complexity: O (1)

1 class Solution2 {3  Public:4     void sortcolors(int A[], int N)5     {6         int Zero = 0,  One = 0,  Both = 0;7          for(int I = 0; I < N; ++ I)8         {9             if(A[I] == 0)Ten             { One                 A[ Both ++] = 2; A                 A[ One ++] = 1; -                 A[Zero ++] = 0; -             } the             Else if(A[I] == 1) -             { -                 A[ Both ++] = 2; -                 A[ One ++] = 1; +             } -             Else +                 A[ Both ++] = 2; A         } at     } - };

Reprint please indicate source: Leetcode---75. Sort Colors


Leetcode---75. Sort Colors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.