第一种思路是计数排序,不过需要两趟才能完成。第二种思路是定义两个index,每次将红色交换至最前,将蓝色交换至最后,白色保持不变,很巧妙的思路。 void sortColors(int A[], int n) { int counts[3] = { 0 }; for (int i = 0; i <            
                
                    
                        
                                                            
                                                                        
                                                                                        原创
                                                                                    
                            2022-01-17 17:43:41
                            
                                77阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2014-11-19 15:23:00
                            
                                72阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...            
                
                    
                        
                                                            
                                                                        
                                                                                        原创
                                                                                    
                            2021-08-07 11:57:00
                            
                                131阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2013-10-01 11:40:00
                            
                                75阅读
                            
                                                                                    
                                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, white and blue.Here, we will use the int            
                
                    
                        
                                                            
                                                                        
                                                                                        原创
                                                                                    
                            2014-11-30 12:51:39
                            
                                438阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            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 r            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2017-06-15 16:13:00
                            
                                73阅读
                            
                                                                                    
                                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, white and blue.Here, we will use the integers            
                
                    
                        
                                                            
                                                                        
                                                                                        原创
                                                                                    
                            2022-08-01 12:21:12
                            
                                88阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2014-03-28 15:29:00
                            
                                102阅读
                            
                                                                                    
                                2评论
                            
                                                 
                 
                
                             
         
            
            
            
            原题链接: http://oj.leetcode.com/problems/sort-colors/ 这道题也是数组操作的题目。事实上就是要将数组排序,仅仅是知道数组中仅仅有三个元素0,1,2。熟悉计数排序的朋友可能非常快就发现这事实上就是使用计数排序,元素空间仅仅须要三个元素就可以。代码例如以下:            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2016-01-27 19:19:00
                            
                                137阅读
                            
                                                                                    
                                2评论
                            
                                                 
                 
                
                             
         
            
            
            
            题目链接:https://leetcode.com/problems/sort-colors/题目:Go            
                
                    
                        
                                                            
                                                                        
                                                                                        原创
                                                                                    
                            2023-07-26 16:48:07
                            
                                69阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            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            
                
                    
                        
                                                            
                                                                        
                                                                                        原创
                                                                                    
                            2022-08-01 17:25:39
                            
                                44阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            Question 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 inte            
                
                    
                        
                                                            
                                                                        
                                                                                        原创
                                                                                    
                            2023-02-02 14:58:47
                            
                                71阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            难度:naive做法60,计数排序 scan两次也是一种办法,第一次把1和2当做一种颜色,放到0的右边,第二次0已经全部在左边,只需把2放到1右边 我们考虑怎么用一次扫描来解决。 Best Solution(Discuss里vote最高): the basic idea is to use two            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2014-09-19 12:58:00
                            
                                91阅读
                            
                                                                                    
                                2评论
                            
                                                 
                 
                
                             
         
            
            
            
            leetcode - Sort Colors            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2015-08-07 17:16:00
                            
                                93阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            class Solution {public: void sortColors(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int n1 = 0, n2 = 0, n3 = 0;              
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2013-07-24 19:40:00
                            
                                31阅读
                            
                                                                                    
                                2评论
                            
                                                 
                 
                
                             
         
            
            
            
            Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2014-06-26 18:02:00
                            
                                57阅读
                            
                                                                             
                 
                
                             
         
            
            
            
            问题描写叙述: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 orde...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2015-12-19 15:31:00
                            
                                122阅读
                            
                                                                                    
                                2评论
                            
                                                 
                 
                
                             
         
            
            
            
            Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2015-01-08 19:11:00
                            
                                47阅读
                            
                                                                                    
                                2评论
                            
                                                 
                 
                
                             
         
            
            
            
            Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2015-02-09 14:19:00
                            
                                100阅读
                            
                                                                                    
                                2评论
                            
                                                 
                 
                
                             
         
            
            
            
            题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍)。思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了。 1 class Solution { 2 public: 3 void sortColors(vector& nums) { 4 ...            
                
                    
                        
                                                            
                                                                        
                                                                                        转载
                                                                                    
                            2015-11-02 20:04:00
                            
                                129阅读
                            
                                                                                    
                                2评论