Sort Colors [leetcode] 扫描数组一遍,O(1)空间复杂度的解法

扫描数组两遍的方法是:第一遍计算有每个颜色有多少个,第二遍再将所有颜色赋回数组

扫描数组一遍的方法:

nextPos数组中记录三种颜色的下一个位置

考虑A={0,2,1,1,0}时我们应该如何更新nextPos

初始:nextPos = {0,0,0}

第一个颜色是0,所以nextPos[0] = 1。A={0...} 但是由于1和2必须在0的后面,所以nextPos[1], nextPos[2]均为1

第二个颜色是2,所以nextPos[2] = 2。A={0, 2...} 1和0均在2的前面,所以不用更新其他值

第三个颜色是1,nextPos[1]++,值为2。同时应把2往后挪一位,A为{0, 1, 2...}。nextPos[2] = 3。

第四个颜色是1,nextPos[1]++,值为3。2还要再往后挪一位,nextPos[2] = 4。

最后一个颜色是0,所以A[1]=0,其他所有颜色均需往后挪一位,A[3] = 1, A[4] = 2。nextPos[1]=4, nextPos[2]=5。

观察以后可以发现当前颜色color = A[i],我们需要将所有大于color的颜色均往后挪,同时对应的nextPos++

可以在一句话里写完:

for (int c = A[i]; c < 3; c++)
      A[nextPos[c]++] = c;

回到A中,i=0的时候,nextPos变成了{1, 1, 1},没有问题,但是A[0]变成2了。所以我们需要将for循环倒过来,使得A[0]仍为0

for (int c = 2; c >= A[i]; c--)
      A[nextPos[c]++] = c;

但是这个循环不会正确运行,因为c=2时将A[0]变成2了,c-- < 2,循环结束。所以我们需要用另外一个变量记录下A[i]最初的值,得到最终的程序:

void sortColors(int A[], int n) {
    int nextPos[3] = {0};
    for (int i = 0; i < n; i++)
    {
        int color = A[i];
        for (int c = 2; c >= color; c--)
            A[nextPos[c]++] = c;
    }
}
时间: 2024-11-02 21:28:08

Sort Colors [leetcode] 扫描数组一遍,O(1)空间复杂度的解法的相关文章

Sort Colors leetcode java

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

Sort Colors —— LeetCode

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

Sort Colors -- leetcode

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 OJ平台上Sort Colors题目算法探讨

原题如下,意思就是说无序数组(由0,1,2组成),一遍扫描,把数组整理成若干个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, white and blue. Here, we will use the integ

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

【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) 30

75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. 注意: 不能使用代码库中的排序函数来解决这道题. 每日一算法2019/6/2Day 30LeetCode75. Sort Colors 示例: 输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2] 进阶: 一个直观的解决方