LeetCode OJ 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.

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?



【题目分析】

一个数组中用0,1,2分别代表三种颜色red,white,blue。对这三种颜色排序,使得red在最前面,blue在最后面。最好的办法是一次遍历,使用固定的空间。



【思路】

1. 排序法

这是效率最低的方法,我们要像排序其他数组一样对有三种值(0,1,2)的数组增序排序,时间复杂度在O(n2);

2. 计数法

先遍历一遍数组,统计每种颜色的个数m,n,l,然后进行第二次遍历,写入m个0,n个1,l个2;时间复杂度为O(n);

3. 标记法

我们设置两个标记,分别记录0的结束位置和2的开始位置,遍历当前数组,如果为1则继续向前,如果为0则把它放到0的结束位置之后,如果为2就把它放到2的开始位置之前。这样经过一遍遍历就可以把不同的颜色分开。举个例子如下:



【java代码】

 1 public class Solution {
 2     public void sortColors(int[] nums) {
 3         if(nums == null || nums.length == 0) return;
 4
 5         int lastred = -1;
 6         int firstblue = nums.length;
 7         int i = 0;
 8
 9         while(i < firstblue){
10             if(nums[i] == 0){
11                 if(i == lastred + 1)
12                     lastred = i++;
13                 else if(i > lastred + 1){
14                     nums[++lastred] = 0;
15                     nums[i++] = 1;
16                 }
17             }
18             else if(nums[i] == 2){
19                 if(nums[firstblue-1] != 2){
20                     nums[i] = nums[firstblue-1];
21                     nums[--firstblue] = 2;
22                 }
23                 else firstblue--;
24             }
25             else i++;
26         }
27     }
28 }
时间: 2025-01-20 05:16:29

LeetCode OJ 75. Sort Colors的相关文章

【一天一道LeetCode】#75. Sort Colors

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 a

LeetCode OJ平台Sort Colors讨论主题算法

原题如下面,这意味着无序排列(由0,1,2组成).一号通.组织成若干阵列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, white and blue. Here, we will use the integers 0

【leetcode】75.Sort Colors

题目说明 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. https://leetcode-cn.com/problems/sort-colors/ 解法1 时间复杂度:O(n) 空间复杂度:O(1) 思路:使用计数排序法,先遍历一遍统计出各个颜色的个数,然后再遍历第二遍,按指定顺序赋值. void sortColors(vector<int>

LeetCode OJ - Insertion Sort List

题目: Sort a linked list using insertion sort. 解题思路: 假设 list[1..i]是排好序的,找到第i+1个元素应该插入的位置及其前驱,然后将其插入. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class S

No.75 Sort Colors

No.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

刷题75. Sort Colors

一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是简单的,代码如下: class Solution{ public: void sortColors(vector<int>& nums){ int num0=0,num1=0,num2=0; for(int i=0;i<nums.size();i++){ if(nums[i]==0)

[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. 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 sort]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