leetcode笔记: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三个数字的数组从小到大排序。

题目要求:come up with an one-pass algorithm using only constant space,因此只能扫描一次。这里采取的方法是,统计0, 1, 2三个数字分别出现的次数,再将数组nums重新构建为从0到2排列的数组,这种方法没有使用数组元素间的交换,只需扫描一次nums。

三. 示例代码

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    void sortColors(vector<int>& nums)
    {
        int SIZE = nums.size();
        int count[3] = {0, 0, 0};
        for (int i = 0; i < SIZE; ++i)
            ++count[nums[i]];

        for (int i = 0, index = 0; i < 3; ++i)
            for (int j = 0; j < count[i]; ++j)
                nums[index++] = i;
    }
};

一个测试结果:

四. 小结

本题的解法还是挺多的,可多参考网上的其他解法。

时间: 2024-10-15 13:22:45

leetcode笔记:Sort Colors的相关文章

【LeetCode】Sort Colors

LeetCode OJ 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, w

[LeetCode OJ] 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 Colors 数组排序

题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组,包含0,1,2分别代表红白蓝三种颜色,要求按照0,1,2的顺序,将同类颜色的连续排列 思路:计数排序,是一个遍历两遍的方法:可以先统计每种的数量,之后直接将这一范围内的所有值都赋值为相应的数字即可 遍历一遍的话可以在遍历的同时分别与0和2比较,从头和尾一起交换,1的在中间不用做处理: * */ package javaTr

[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 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, a

leetcode No75. Sort Colors

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 integers 0, 1, and 2 to represent the color red, whi

leetcode 【 Sort Colors 】python 实现

题目: 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, an

【leetcode】Sort Colors(middle)☆

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