LeetCode 75

Sort Colors

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

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

Note:
You are not 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 an one-pass algorithm using only constant space?

  1 /*************************************************************************
  2     > File Name: LeetCode075.c
  3     > Author: Juntaran
  4     > Mail: JuntaranMail@gmail.com
  5     > Created Time: Tue 19 May 2016 20:41:54 PM CST
  6  ************************************************************************/
  7 
  8 /*************************************************************************
  9     
 10     Sort Colors
 11     
 12     Given an array with n objects colored red, white or blue, 
 13     sort them so that objects of the same color are adjacent, 
 14     with the colors in the order red, white and blue.
 15 
 16     Here, we will use the integers 0, 1, and 2 to represent the color red, 
 17     white, and blue respectively.
 18 
 19     Note:
 20     You are not suppose to use the library's sort function for this problem.
 21 
 22     click to show follow up.
 23 
 24     Follow up:
 25     A rather straight forward solution is a two-pass algorithm 
 26     using counting sort.
 27     First, iterate the array counting number of 0's, 1's, and 2's, 
 28     then overwrite array with total number of 0's, then 1's and followed by 2's.
 29 
 30     Could you come up with an one-pass algorithm using only constant space?
 31 
 32  ************************************************************************/
 33 
 34 #include <stdio.h>
 35 #include <stdlib.h>
 36 
 37 void printNums(int* nums, int numsSize)
 38 {
 39     int i;
 40     for( i=0; i<numsSize; i++ )
 41     {
 42         printf("%d ",nums[i]);
 43     }
 44     printf("
");
 45 }
 46 
 47 /* 高端做法 */
 48 void sortColors(int* nums, int numsSize)
 49 {
 50     int red=-1, white=-1, blue=-1;
 51     int i;
 52 
 53     printNums(nums, numsSize);
 54     for( i=0; i<numsSize; i++ )
 55     {
 56         if(nums[i] == 0)
 57         {
 58             nums[++blue] =2;
 59             nums[++white]=1;
 60             nums[++red]  =0;
 61             printNums(nums, numsSize);
 62         }
 63         else if (nums[i] == 1)
 64         {
 65             nums[++blue] =2;
 66             nums[++white]=1;
 67             printNums(nums, numsSize);
 68 
 69         }
 70         else if (nums[i] == 2)
 71         {
 72             nums[++blue] =2;
 73             printNums(nums, numsSize);
 74         }
 75     }
 76 }
 77 
 78 
 79 /*
 80 
 81 ------- Before meeting nums[3] -------
 82 index:  0 1 2 3 4 5 6 7 8
 83 nums:   0 1 2 1 2 0 2 2 1
 84 lables:   r w b
 85 
 86 paint:  2 2 2 
 87         1 1   
 88         0
 89 
 90 appear: 0 1 2 
 91 
 92 ------- After dealing with nums[3] -------
 93 index:  0 1 2 3 4 5 6 7 8
 94 nums:   0 1 2 1 2 0 2 2 1
 95 lables:   r   w b
 96 
 97 paint:  2 2 2 2
 98         1 1 1  
 99         0
100 
101 appear: 0 1 1 2
102 
103 ------- After finish iteration -------
104 index:  0 1 2 3 4 5 6 7 8
105 nums:   0 1 2 1 2 0 2 2 1
106 lables:     r     w       b
107 
108 paint:  2 2 2 2 2 2 2 2 2
109         1 1 1 1 1  
110         0 0 
111 
112 appear: 0 0 1 1 1 2 2 2 2
113 
114 */
115 
116 
117 /* 智障做法 */
118 void sortColors2(int* nums, int numsSize)
119 {
120     int red = 0;
121     int white = 0;
122     int blue = 0;
123     
124     int i;
125     for( i=0; i<numsSize; i++ )
126     {
127         if( nums[i] == 0 )
128         {
129             red ++;
130         }
131         if( nums[i] == 1 )
132         {
133             white ++;
134         }
135         if( nums[i] == 2 )
136         {
137             blue ++;
138         }
139     }
140 //    printf("%d %d %d
", red,white,blue);
141     
142     for( i=0; i<red; i++ )
143     {
144         nums[i] = 0;
145     }
146 //    printNums(nums, numsSize);
147     
148     for( i=red; i<white+red; i++ )
149     {
150         nums[i] = 1;
151     }
152 //    printNums(nums, numsSize);
153     
154     for( i=white+red; i<white+red+blue; i++ )
155     {
156         nums[i] = 2;
157     }
158 //    printNums(nums, numsSize);
159 }
160 
161 
162 
163 int main()
164 {
165     int nums[] = {1,0,1,2,1,0,2,0,2};
166     int numsSize = 9;
167     sortColors(nums, numsSize);
168 
169     return 0;
170 }
原文地址:https://www.cnblogs.com/Juntaran/p/5511674.html