75. Sort Colors (Array)

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?

思路: 使用两个指针,分别指向最后一个红色的后一个位置,以及第一个蓝色蓝色的前一个位置。如果当前扫描到的元素是红色,那么与第一个指针互换,若为蓝色,与蓝色头部指针互换,若为白色,不做任何操作。

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int n = nums.size();
        int redTail = 0;
        int blueHead = n-1;
        int tmp;

        for(int i = 0; i <= blueHead;){
            if(nums[i]==0){//当前是红色
                if(nums[redTail]==0){//红色尾指针指向红色(说明没有白色)
                    redTail++;
                    i++; //不处理当前元素
                    continue;
                }

                tmp = nums[redTail];
                nums[redTail] = nums[i];
                redTail++;
                if(tmp==1){ //红色尾指针指向白色
                    nums[i] = tmp; //交换红色和白色
                    i++;
                }
                else{ //红色尾指针指向蓝色(说明目前没有白色)
                    nums[i] = nums[blueHead]; //把blueHead处的元素放到i
                    nums[blueHead]=tmp; //把蓝色放到blueHead处
                    blueHead--;
                }
            }
            else if(nums[i]==1){//如果当前是白色,不处理当前元素
                i++;
            }
            else{ //如果当前是蓝色
                tmp = nums[blueHead];
                nums[blueHead] = nums[i];//把蓝色放到blueHead处
                nums[i] = tmp;//把blueHead处的元素放到i
                blueHead--;
            }
        }
    }
};
时间: 2024-12-10 01:13:53

75. Sort Colors (Array)的相关文章

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)

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

LeetCode 75 Sort Colors(颜色排序)

翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字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, whit

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

75.Sort Colors(法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 inthe order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red,white, and blue

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 bl