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

对3种颜色进行排序,题目要求不能使用sort function,follow up中说可以有two pass的解法,计数统计用HashMap或Array,先迭代计数0,1,2的个数,然后在over write到数组。又问能否用one pass的解法,这要利用只有3个颜色的特点,使用双指针left, right来记录处理过的0和2的边界位置,一开始,两个指针分别指向头和尾,然后遍历数组。当遇到0时,和left指针的数交换,然后将left指针向右移1位。当遇到2时,和右指针的数交换,然后将右指针向左移1位。遇到1时,不做处理,进入到下一数。right指针时,就已经排好序了,所有0都被交换到了前面,所有2都被交换到了后面。

Java: one pass

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

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)

C++: two pass, Time: O(n), Space: O(1)

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)

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--]);
            }
        }
    }
};

  

  

原文地址:https://www.cnblogs.com/lightwindy/p/8495662.html

时间: 2024-11-07 22:04:25

[LeetCode] 75. Sort Colors 颜色排序的相关文章

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 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 的个数 时间复杂度: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 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] 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

排序算法之快速排序(Quick Sort) -- 适用于Leetcode 75 Sort Colors

Quick Sort使用了Divide and Concur的思想: 找一个基准数, 把小于基准数的数都放到基准数之前, 把大于基准数的数都放到基准数之后 Worst case: O(n^2) Average case: O(nlogN) 步骤: 初始的数组 Array a[]: 0 1 2 3 4 5 6 7 8 9 51 73 52 18 91 7 87 73 48 3 基准数: X = a[0] = 51 i 的值: i = 0 j 的值: j = 9 (a.length) Step 1:

[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

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