[Leetcode] 75. Sort Colors Java

Source: Internet
Author: User

Topic:

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.

Click to show follow up.

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?

test instructions and analysis: an array containing 0,1,2, sorted so that all 0,1,2 are separated together. It is best to traverse only once and require a space complexity of O (1);

Code One : traverse the array to calculate the number of occurrences of the 0,1,2 and then replace it in the Nums, which uses two traversal

1 classSolution {2      Public voidSortcolors (int[] nums) {3         intOnecount = 0,twocount=0,zerocount=0;4          for(inti=0;i<nums.length;i++){5             if(nums[i]==0) zerocount++;6             if(nums[i]==1) onecount++;7             if(nums[i]==2) twocount++;8         }9          for(inti=0;i<zerocount;i++){TenNums[i] = 0; One         } A          for(inti=zerocount;i<zerocount+onecount;i++){ -Nums[i] = 1; -         } the          for(inti=zerocount+onecount;i<zerocount+onecount+twocount;i++){ -Nums[i] = 2; -         } -     } +}

Code two: use only one traversal, double pointer method, with a start and an end to point to the array's front and back elements, traverse the array, and if you encounter 0 and start interchange, encounter 2 and end Exchange

classSolution { Public voidSortcolors (int[] nums) {       intStart = 0; intEnd = Nums.length-1; intI=0;  while(i<=end) {            if(nums[i]==0) {//swap 0 to the front                inttmp=Nums[i]; Nums[i]=Nums[start]; Nums[start]=tmp; Start++; I++; }            Else if(nums[i]==2) {//Swap 2 to the back                inttemp =Nums[i]; Nums[i]=Nums[end]; Nums[end]=temp; End--; }            Else     //Skip overi++; }    }}

Code Three: Translation method

Class Solution {public    void Sortcolors (int[] nums) {       int i=-1,j=-1,k=-1;        for (int x=0;x<nums.length;x++) {            if (nums[x]==0) {                nums[++k] = 2;                NUMS[++J] = 1;                Nums[++i] = 0;            } else if (nums[x]==1) {                nums[++k] = 2;                NUMS[++J] = 1;            } else {                Nums[++k] = 2;            }        }    }}

  

[Leetcode] 75. Sort Colors Java

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.