[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, and blue respectively.

Note:
You are not suppose to use the library‘s sort function for this problem.

2 思路

好吧,这题是我见过最简单的,居然是中等难度,只要之前听说过这种类型的题目,都会做。就是count sort法。

数1,2,0的个数,然后再遍历赋值就行了。

3 代码

public void sortColors(int[] nums) {
        int redNumber = 0;
        int whiteNumber = 0;
        for(int i = 0;i < nums.length; i++){
            if(nums[i] == 0){
                redNumber++;
            }else if(nums[i]==1){
                whiteNumber++;
            }
        }
        for(int i = 0; i < redNumber; i++){
            nums[i] = 0;
        }
        for(int i = redNumber; i < redNumber + whiteNumber; i++){
            nums[i] = 1;
        }
        for(int i = redNumber + whiteNumber; i < nums.length; i++){
            nums[i] = 2;
        }
    }

遍历一遍的方法:https://leetcode.com/discuss/17000/share-my-one-pass-constant-space-10-line-solution

the idea is to sweep all 0s to the left and all 2s to the right, then all 1s are left in the middle.

class Solution {
    public:
        void sortColors(int A[], int n) {
            int second=n-1, zero=0;
            for (int i=0; i<=second; i++) {
                while (A[i]==2 && i<second) swap(A[i], A[second--]);
                while (A[i]==0 && i>zero) swap(A[i], A[zero++]);
            }
        }
    };
时间: 2024-10-27 00:44:57

[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(颜色排序)

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

[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

排序算法之快速排序(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 计数排序,三路快排

解法一:计数排序:统计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

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

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