[LeetCode] Sort Colors 解题报告
来源:互联网 发布:淘宝网羽绒服女装 编辑:程序博客网 时间:2024/06/05 08:52
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.
You are not 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.
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?
» Solve this problem
[解题思路]
一开始想到的就是计数排序,但是计数排序需要两边扫描,第一遍统计红,白,蓝的数目,第二遍生成新数组。
考虑到题目要求one pass。这就意味着类似于链表的双指针问题,这里也要track两个index,一个是red的index,一个是blue的index,两边往中间走。
i从0到blue index扫描,
遇到0,放在red index位置,red index后移;
遇到2,放在blue index位置,blue index前移;
遇到1,i后移。
扫描一遍得到排好序的数组。时间O(n),空间O(1),
[Code]
two-pass solution, Counting sort solution
1: void sortColors(int A[], int n) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: int red=0, white =0, blue=0;
5: for(int i =0; i < n; i++)
6: {
7: switch(A[i])
8: {
9: case 0:
10: red++;break;
11: case 1:
12: white++;break;
13: case 2:
14: blue++;break;
15: }
16: }
17: for(int i =0; i<n; i++)
18: {
19: if(red>0)
20: {
21: A[i]=0;
22: red--;
23: continue;
24: }
25: if(white>0)
26: {
27: A[i] =1;
28: white--;
29: continue;
30: }
31: A[i]=2;
32: }
33: }
one-pass, two pointers solution
1: void sortColors(int A[], int n) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: int redSt=0, bluSt=n-1;
5: int i=0;
6: while(i<bluSt+1)
7: {
8: if(A[i]==0)
9: {
10: std::swap(A[i],A[redSt]);
11: redSt++;
12: i++;
13: continue;
14: }
15: if(A[i] ==2)
16: {
17: std::swap(A[i],A[bluSt]);
18: bluSt--;
19: continue;
20: }
21: i++;
22: }
23: }
0 0
- 【LeetCode】Sort Colors 解题报告
- [LeetCode] Sort Colors 解题报告
- [leetcode] 75. Sort Colors 解题报告
- leetcode解题报告:75. Sort Colors
- [Leetcode] 75. Sort Colors 解题报告
- Leetcode 75. Sort Colors 色彩排序 解题报告
- [LeetCode]Sort List,解题报告
- LeetCode Sort List 解题报告
- Leetcode Sort List 解题报告
- LeetCode 解题报告 Sort List
- LeetCode Sort List 解题报告
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
- LeetCode—Sort List解题报告
- LeetCode—Insertion Sort List 解题报告
- Leetcode Insertion Sort List 解题报告
- leetCode解题报告之Insertion Sort List
- leetCode解题报告之Sort List
- Leetcode Insertion Sort List 解题报告
- git管理相关命令
- [LeetCode] Search Insert Position 解题报告
- [LeetCode] Set Matrix Zeroes 解题报告
- [LeetCode] Simplify Path 解题报告
- Mac 装双系统
- [LeetCode] Sort Colors 解题报告
- Unity Shader中顶点坐标的大小似乎会受到Draw Call合批的影响
- java +号字符串拼接原理解释以及StringBulider比较
- [LeetCode] Spiral Matrix 解题报告
- [LeetCode] Sqrt(x) 解题报告
- [LeetCode] String to Integer (atoi) 解题报告
- [LeetCode] Subsets 解题报告
- [LeetCode] Subsets II 解题报告
- 提交Hadoop作业失败,报错Connection refused