[Leetcode] 75. Sort Colors Color Sorting

Source: Internet
Author: User

Given an array with n objects colored red, white or blue, sort them so, objects of the same color is Adjacen T, with the 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?

To sort 3 colors, the problem requires that the sort function,follow up can have two pass solutions, count statistics with HashMap or array, iterate over the count 0,1,2 number, and then in the over write to the array. Ask if you can use the one pass solution, which takes advantage of only 3 colors, using the double pointer left and right to record the processed 0 and 2 boundary positions, starting with two pointers pointing to the head and tail, and then traversing the array. When it encounters 0 o'clock, the number of the left pointer is exchanged, and then the left pointer is shifted 1 bits to the right. When 2 o'clock is encountered, the number of right pointers is exchanged, then the right pointer is shifted to the left by 1 bits. Encounters 1 o'clock, does not do the processing, enters to the next number. Right pointer, it is already in order, all 0 are swapped to the front, all 2 are swapped to the back.

Java:one Pass

public class Solution {public    void Sortcolors (int[] nums) {        int left = 0, right = nums.length-1;        int i = 0;        while (I <= right) {            if (nums[i] = = 0) {                swap (nums, I, left);                left++;                i++; Because the left side has been checked, you can go to the next            } else if (nums[i] = = 2) {                swap (nums, I, right);                right--; Because the number of exchanges has not been checked, so can not i++            } else {                i++            ;        }}} private void Swap (int[] nums, int i1, int i2) {        int tmp = NUMS[I1];        NUMS[I1] = Nums[i2];        NUMS[I2] = tmp;    }}

Python:one Pass

Class solution (Object):    def sortcolors (self, nums):        def tripartition (Nums, target): Left            , idx, right = 0, 0, Len (nums)-1 while                        idx <= right:                if NUMS[IDX] < target:                    Nums[left], nums[idx] = Nums[idx], nums[left]
   left + = 1                    idx + 1                elif Nums[idx] > target:                    nums[idx], nums[right] = Nums[right], Nums[idx] right                    - = 1                Else:                    idx + = 1                            tripartition (nums, 1)

C + +: Both pass, Time:o (n), Space:o (1)

Class Solution {public:    void sortcolors (int a[], int n) {        int count[3] = {0}, idx = 0;        for (int i = 0; i < n; ++i) ++count[a[i];        for (int i = 0, i < 3; ++i) {for            (int j = 0; J < Count[i]; ++j) {                a[idx++] = i;            }}}    ;

C + +: One pass, Time:o (n), Space:o (1)

Class Solution {public:    void sortcolors (int a[], int n) {
int red = 0, blue = n-1; for (int i = 0; I <= blue; ++i) { if (a[i] = = 0) { swap (a[i], a[red++]), } else if (a[i] = = 2) { SW AP (a[i--], a[blue--]);}}} ;

  

  

[Leetcode] 75. Sort Colors Color Sorting

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.