Leetcode OJ 75. Sort Colors

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?

"Problem Analysis"

An array with 0,1,2 represents three different color red,white,blue, respectively. Sort these three colors so that red is at the top and blue is the last. The best way to do this is to traverse it once, using a fixed space.

Ideas

1. Sorting method

This is the least efficient method, and we want to sort the array with three values (0,1,2) in the same order as the other arrays, with the time complexity in O (N2);

2. Counting method

Go through the array first, count the number of each color m,n,l, and then perform a second traversal, write M 0,n 1,l 2, Time complexity O (n);

3. Labeling method

We set two markers to record the end position of 0 and the beginning of 2, to traverse the current array, if 1 to continue forward, or 0 to put it to the end of 0, if 2 would put it before the start of 2. This way you can separate different colors by traversing them over and over again. Here's an example:

"Java Code"

1  Public classSolution {2      Public voidSortcolors (int[] nums) {3         if(Nums = =NULL|| Nums.length = = 0)return;4         5         intlastred =-1;6         intFirstblue =nums.length;7         inti = 0;8         9          while(I <firstblue) {Ten             if(Nums[i] = = 0){ One                 if(i = = lastred + 1) Alastred = i++; -                 Else if(i > lastred + 1){ -Nums[++lastred] = 0; thenums[i++] = 1; -                 } -             } -             Else if(Nums[i] = = 2){ +                 if(nums[firstblue-1]! = 2){ -Nums[i] = nums[firstblue-1]; +Nums[--firstblue] = 2; A                 } at                 Elsefirstblue--; -             } -             Elsei++; -         } -     } -}

Leetcode OJ 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.