题目描述
- 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 c

这篇博客探讨了如何解决荷兰国旗问题,即给定一个包含红色、白色和蓝色对象的数组,需要通过一次遍历来排序使得相同颜色的对象相邻,并按红色、白色、蓝色的顺序排列。文中介绍了不能使用库排序函数的限制,并提出了一种一-pass算法,通过设置三个指针,分别处理0、1、2的元素,实现了在常数空间内的排序。
&spm=1001.2101.3001.5002&articleId=85175144&d=1&t=3&u=27fdf8b076bf46a89031f674a694dce5)
3651

被折叠的 条评论
为什么被折叠?



