Leetcode 75. Color classification

Source: Internet
Author: User

Given an array of n elements that contain red, white, and blue, sort them in place so that elements of the same color are adjacent and arranged in red, white, and blue order.

In this question, we use integers 0, 1, and 2 to denote red, white, and blue, respectively.

Attention:
You cannot use the sort function in the code base to solve this problem.

Example:

Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2]

Advanced:

      • An intuitive solution is a two-pass scanning algorithm using counting sequencing.
        First, the iteration calculates the number of elements 0, 1, and 2, and then overrides the current array by sorting 0, 1, and 2.
      • Can you think of a scan algorithm that uses only constant space?
Partition

Divide the list elements into three categories: equal to one, greater than one, less than one. Can be implemented with three-way partition.

Before introducing the three-way partition, first review the classic two-way partition implementation. As in the introduction of similar algorithms, the version:

intPartition (vector<int>&arr,intLowintHigh ) {    intPivot = Arr[low];//Select the first element as a pivot element    intlocation = low;//Location points to the tail of the element segment smaller than pivot     for(inti = low +1; I <= high; i++)//elements smaller than the pivot element are placed in the first half of the sequence        if(Arr[i] <=pivot) Swap (Arr[i], arr[++Location ]);    Swap (Arr[low], arr[location]); returnLocation ;}

Partition returns the subscript, subscript and subscript to the left is all <=pivot. If you want all <pivot, you can change the less than equals sign in if to less than sign.

The second is a double pointer and a pivot from the left of the array:

intMypartition (vector<int>&arr,intLowintHigh ) {    intPivot = Arr[low];//Select the first element as a pivot element     while(Low <High ) {         while(Low < High && Arr[high] >= pivot) high--; Arr[low]= Arr[high];//find the first element less than pivot from behind and put it in the low position         while(Low < High && Arr[low] <= pivot) low++; Arr[high]= Arr[low];//find the first element greater than pivot from the front and put it in the high position} Arr[low]= pivot;//Finally, the pivot element is placed in the low position    returnLow ;}

For this problem, the three-way partition are as follows:

classSolution { Public:    voidSortcolors (vector<int> &nums) {        intZero =-1;//[0...zero] = = 0        intboth = Nums.size ();//[two...n-1] = = 2         for(inti =0; I <A .) {            if(Nums[i] = =1) I++; Else if(Nums[i] = =2) Swap (nums[i], nums[--(both ]); Else{//Nums[i] = = 0ASSERT (nums[i] = =0 ); Swap (nums[++zero], nums[i++] ); }        }    }}; 

Leetcode 75. Color classification

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.