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: [email protected]
  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("\n");
 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\n", 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 }
时间: 2024-11-10 01:11:23

LeetCode 75的相关文章

LeetCode 75. 颜色分类(Sort Colors) 30

75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. 注意: 不能使用代码库中的排序函数来解决这道题. 每日一算法2019/6/2Day 30LeetCode75. Sort Colors 示例: 输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2] 进阶: 一个直观的解决方

[leetcode 75] Sort Colors

1 题目 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, a

[LeetCode]75. Pow(x,n)幂运算

Implement pow(x, n). Subscribe to see which companies asked this question 解法1:最简单的即是n个x直接相乘,毫无疑问会超时Time Limit Exceeded class Solution { public: double myPow(double x, int n) { if(x < 0.000001) return 0; if(n == 0) return 1; if(n < 0) return 1.0 / my

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 bl

LeetCode——75. 颜色分类

75. 颜色分类 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. 注意: 不能使用代码库中的排序函数来解决这道题. 示例: 输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2] 进阶: 一个直观的解决方案是使用计数排序的两趟扫描算法. 首先,迭代计算出0.1 和 2 元素的个数,然后按照0.1.2的排序,重写当前数组. 你能想出

排序算法之快速排序(Quick Sort) -- 适用于Leetcode 75 Sort Colors

Quick Sort使用了Divide and Concur的思想: 找一个基准数, 把小于基准数的数都放到基准数之前, 把大于基准数的数都放到基准数之后 Worst case: O(n^2) Average case: O(nlogN) 步骤: 初始的数组 Array a[]: 0 1 2 3 4 5 6 7 8 9 51 73 52 18 91 7 87 73 48 3 基准数: X = a[0] = 51 i 的值: i = 0 j 的值: j = 9 (a.length) Step 1:

LeetCode 75 Sort Colors(颜色排序)

翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 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, whit

[leetcode]75.Sort Color三指针

import java.util.Arrays; /** * 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 repr

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 bl