[LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place 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.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
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 a one-pass algorithm using only constant space?
对3种颜色进行排序,用0,1,2三个数字代表不同颜色。题目要求不能使用sort function。
的解法1:counting sort计数统计,two pass。先用HashMap或Array统计每个数字的个数,然后分别迭代0,1,2的个数,over write到数组。
解法2:双指针two pointers,one pass。如果算上迭代用的index,就有三个指针。利用只有3个颜色的特点,使用left, right双指针来指向记录处理过的0和2的边界位置。一开始,两个指针分别指向头和尾,然后遍历数组。当遇到0时,和left指针的数交换,然后将left指针向右移1位。当遇到2时,和右指针的数交换,然后将右指针向左移1位。遇到1时,不做处理,进入到下一数。当遇到right指针时,就已经排完序了,所有0都被交换到了前面,所有2都被交换到了后面。
Java: one pass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Solution { public void sortColors( int [] nums) { int left = 0 , right = nums.length - 1 ; int i = 0 ; while (i <= right){ if (nums[i] == 0 ){ swap(nums, i, left); left++; i++; // 因为左边的已经检查过,所以可以进入下一个 } else if (nums[i] == 2 ){ swap(nums, i, right); right--; // 因为交换过来的数还没检查过,所以不能i++ } else { i++; } } } private void swap( int [] nums, int i1, int i2){ int tmp = nums[i1]; nums[i1] = nums[i2]; nums[i2] = tmp; } } |
Python: one pass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution( object ): def sortColors( self , nums): def triPartition(nums, target): left, idx, right = 0 , 0 , len (nums) - 1 while idx < = right: if nums[idx] < target: nums[left], nums[idx] = nums[idx], nums[left] left + = 1 idx + = 1 elif nums[idx] > target: nums[idx], nums[right] = nums[right], nums[idx] right - = 1 else : idx + = 1 triPartition(nums, 1 ) |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution( object ): def sortColors( self , nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ i, j, k = 0 , 0 , len (nums) - 1 while j < = k: # while j < len(nums): or while j < k: if nums[j] = = 0 : nums[i], nums[j] = nums[j], nums[i] i + = 1 j + = 1 # important elif nums[j] = = 2 : nums[j], nums[k] = nums[k], nums[j] k - = 1 else : j + = 1 |
Python: one pass, no swap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Solution( object ): def sortColors( self , nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ i = j = 0 for k in xrange ( len (nums)): v = nums[k] nums[k] = 2 if v < 2 : nums[j] = 1 j + = 1 if v = = 0 : nums[i] = 0 i + = 1 |
C++: two pass, Time: O(n), Space: O(1)
1 2 3 4 5 6 7 8 9 10 11 12 | class Solution { public : void sortColors( int A[], int n) { int count[3] = {0}, idx = 0; for ( int i = 0; i < n; ++i) ++count[A[i]]; for ( int i = 0; i < 3; ++i) { for ( int j = 0; j < count[i]; ++j) { A[idx++] = i; } } } }; |
C++: one pass, Time: O(n), Space: O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution { public : void sortColors( int A[], int n) { int red = 0, blue = n - 1; for ( int i = 0; i <= blue; ++i) { if (A[i] == 0) { swap(A[i], A[red++]); } else if (A[i] == 2) { swap(A[i--], A[blue--]); } } } }; |
C++:
1 2 3 4 5 6 7 8 9 10 | class Solution { public : void sortColors( int A[], int n) { int second=n-1, zero=0; for ( int i=0; i<=second; i++) { while (A[i]==2 && i<second) swap(A[i], A[second--]); while (A[i]==0 && i>zero) swap(A[i], A[zero++]); } } }; |
类似题目:
All LeetCode Questions List 题目汇总
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞