Leetcode:Sort colors 计数排序

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.

题解分析:

当我们已知待排序元素的值范围,可以有 T(n) = O(n)的排序方法

这个题目中,很显然,待排元素的值范围已知,其他的诸如 对学生成绩排序,对职工年龄排序 这些都可以应用计数排序

计数排序的基本思想是对每一个输入元素x,确定出小于x的元素的个数。然后再将x直接放置在它在最终输出数组中的位置上。

class Solution {
public:
    void sortColors(int A[], int n) {
        assert(A != NULL && n >= 0);
        auto result = minmax_element(A, A + n);
        int minVal = *result.first;
        int maxVal = *result.second;
        int totalCount = maxVal - minVal + 1;
        if (totalCount == 1) return;
        int* count = new int[totalCount]();

        for (int i = 0; i < n; ++i) {
            ++(count[A[i] - minVal]);
        }

        for (int i = 1; i < totalCount; ++i) {
            count[i] += count[i-1];
        }

        int* tmp = new int[n]();
        for (int i = n - 1; i >= 0; --i) {   // 必须从后往前,否则不是稳定的算法
            tmp[count[A[i] - minVal] - 1] = A[i];
            --(count[A[i] - minVal]);
        }

        copy(tmp, tmp + n, A);

        delete[] count;
        delete[] tmp;
    }
};

动画演示:http://www.cs.usfca.edu/~galles/visualization/CountingSort.html

Leetcode:Sort colors 计数排序

时间: 2024-10-12 19:50:01

Leetcode:Sort colors 计数排序的相关文章

[C++]LeetCode: 127 Sort Colors (计数排序 &amp; 快速排序)

题目: 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 75 Sort Colors 计数排序,三路快排

解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortColors(vector<int>& nums) { int count[3] = {0}; //存放0,1,2三个元素的频率 for(int i=0;i<nums.size();i++){ assert(nums[i] >=0 && nums[i]<=2

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

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

[leetcode]Sort Colors @ Python

原题地址:https://oj.leetcode.com/problems/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 integer

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 bl

LeetCode: Sort Colors [075]

[题目] 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 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 [23]

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