【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 blue respectively.

颜色排序

思路:

直觉看一定有简单的方法的,可惜我没想出来,就复习一下二分归并排序吧。

    void sortColors(int A[], int n) {
        MergeSort(A, 0, n - 1);
    }

    void MergeSort(int A[], int l, int r)
    {
        if(l < r)
        {
            int mid = (l + r) / 2;
            MergeSort(A, l, mid);
            MergeSort(A, mid + 1, r);
            Merge(A, l, mid, r);
        }
    }
    void Merge(int A[], int l, int mid, int r)
    {
        int x = mid - l + 1;
        int y = r - mid;
        int * B = new int[x];
        int * C = new int[y];
        memcpy(B, A + l, x * sizeof(int));
        memcpy(C, A + mid + 1, y * sizeof(int));
        int i = 0, j = 0, k = l;
        while(i < x && j < y)
        {
            if(B[i] < C[j])
                A[k] = B[i++];
            else
                A[k] = C[j++];
            k++;
        }
        if(i >= x)
            memcpy(A + k, C + j, (y - j) * sizeof(int));
        else
            memcpy(A + k, B + i, (x - i) * sizeof(int));

        delete [] B;
        delete [] C;
    }

看看大神的线性时间、常量空间的方法

①说实话,我一眼望过去看不懂啊。研究了半天,发现 n0是最后一个0个位置, n1是最后一个1的位置, n2是最后一个2的位置

每次新来一个0,需要依次把2、1向后延1位。

就是2先向后多占一个位置,然后1向后多占一个位置,把2最前面的给覆盖,0再多占一个位置,新加的0覆盖了1最前面多出来的。

依此类推。

void sortColors2(int A[], int n) {
    int n0 = -1, n1 = -1, n2 = -1;
    for (int i = 0; i < n; ++i) {
        if (A[i] == 0)
        {
            A[++n2] = 2;
            A[++n1] = 1;
            A[++n0] = 0;
        }
        else if (A[i] == 1)
        {
            A[++n2] = 2;
            A[++n1] = 1;
        }
        else if (A[i] == 2)
        {
            A[++n2] = 2;
        }
    }
时间: 2024-08-02 19:23:25

【leetcode】Sort Colors(middle)☆的相关文章

【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】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】Sort Colors 数组排序

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

【LeetCode】【Python】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 list 单链表的归并排序

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排序,链表找到中点的方法 存在的缺点:边界条件多考虑!!! /** * LeetCode Sort List Sort a linked list in O(n log n) time using constant space complexity. * 题目:将一个单链表进行排序,时间复杂度要求为o

【数组】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, an

【Leetcode】Sort List JAVA实现

Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代码实现 package com.edu.leetcode; import com.edu.leetcode.ListNode; public class SortList { /** * @param args */ public ListNode Merge(ListNode first,List

【Leetcode】Sort List in java,你绝对想不到我是怎么做的^^我写完过了我自己都觉得好jian~

Sort a linked list in O(n log n) time using constant space complexity. 大家看完题目估计跟我一样啦...都在想哪些是nlogn啊~快速排序.归并排序.堆排序!然后开始愁,这些东西变成list了可怎么办啊... 可是我深深地记得在CMU的时候老师告诉我,java现在自带的Arrays.sort用的是快排,然后我就想,那么--为什么不把list转成array然后排完了放回去呢! 于是我就用一个arraylist先装这个list,然

【leetcode】sort list(python)

链表的归并排序 超时的代码 class Solution: def merge(self, head1, head2): if head1 == None: return head2 if head2 == None: return head1 # head1 and head2 point to the same link list if head1 == head2: return head1 head = None tail = None # the small pointer point