LeetCode 75

时间:2023-03-09 06:37:09
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?

 /*************************************************************************
> File Name: LeetCode075.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 19 May 2016 20:41:54 PM CST
************************************************************************/ /************************************************************************* 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? ************************************************************************/ #include <stdio.h>
#include <stdlib.h> void printNums(int* nums, int numsSize)
{
int i;
for( i=; i<numsSize; i++ )
{
printf("%d ",nums[i]);
}
printf("\n");
} /* 高端做法 */
void sortColors(int* nums, int numsSize)
{
int red=-, white=-, blue=-;
int i; printNums(nums, numsSize);
for( i=; i<numsSize; i++ )
{
if(nums[i] == )
{
nums[++blue] =;
nums[++white]=;
nums[++red] =;
printNums(nums, numsSize);
}
else if (nums[i] == )
{
nums[++blue] =;
nums[++white]=;
printNums(nums, numsSize); }
else if (nums[i] == )
{
nums[++blue] =;
printNums(nums, numsSize);
}
}
} /* ------- Before meeting nums[3] -------
index: 0 1 2 3 4 5 6 7 8
nums: 0 1 2 1 2 0 2 2 1
lables: r w b paint: 2 2 2
1 1
0 appear: 0 1 2 ------- After dealing with nums[3] -------
index: 0 1 2 3 4 5 6 7 8
nums: 0 1 2 1 2 0 2 2 1
lables: r w b paint: 2 2 2 2
1 1 1
0 appear: 0 1 1 2 ------- After finish iteration -------
index: 0 1 2 3 4 5 6 7 8
nums: 0 1 2 1 2 0 2 2 1
lables: r w b paint: 2 2 2 2 2 2 2 2 2
1 1 1 1 1
0 0 appear: 0 0 1 1 1 2 2 2 2 */ /* 智障做法 */
void sortColors2(int* nums, int numsSize)
{
int red = ;
int white = ;
int blue = ; int i;
for( i=; i<numsSize; i++ )
{
if( nums[i] == )
{
red ++;
}
if( nums[i] == )
{
white ++;
}
if( nums[i] == )
{
blue ++;
}
}
// printf("%d %d %d\n", red,white,blue); for( i=; i<red; i++ )
{
nums[i] = ;
}
// printNums(nums, numsSize); for( i=red; i<white+red; i++ )
{
nums[i] = ;
}
// printNums(nums, numsSize); for( i=white+red; i<white+red+blue; i++ )
{
nums[i] = ;
}
// printNums(nums, numsSize);
} int main()
{
int nums[] = {,,1,,,,,,};
int numsSize = ;
sortColors(nums, numsSize); return ;
}