leetcode 75. Sort Colors (快排/双指针)


Given an array with n objects colored red, white or blue, sort them in-place 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.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
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 a one-pass algorithm using only constant space?

QuickSort

C++

class Solution {
public:
    void sortColors(vector<int>& a) {
        int n=a.size();
        if(n<=1)return;
        qs(a,0,n-1);
    }
    void qs(vector<int>& a,int l,int r){
        if(r<=l)return ;
        int m=part(a,l,r);
        qs(a,l,m-1);
        qs(a,m+1,r);
    }
    int part(vector<int>& a,int l,int r){
        if(l==r)return l;
        int pivot=a[r],i=l;
        for(int j=l;j<r;j++)
            if(a[j]<=pivot)
                swap(a[j],a[i++]);
        swap(a[r],a[i]);
        return i;
    }
};

Java

class Solution {
    public void sortColors(int[] a) {
        int n=a.length;
        if(n<=1)return ;
        qs(a,0,n-1);
    }
    public void qs(int[] a,int l,int r){
        if(r<=l)return;
        int m=part(a,l,r);
        qs(a,l,m-1);
        qs(a,m+1,r);
    }
    public int part(int[] a,int l,int r){
        if(l==r)return l;
        int pivot=a[r],i=l;
        for(int j=l;j<r;j++)
            if(a[j]<=pivot){
                int tmp=a[j];
                a[j]=a[i];
                a[i]=tmp;
                i+=1;
            }
        int tmp=a[r];
        a[r]=a[i];
        a[i]=tmp;
        return i;
    }
}

Python

class Solution:
    def sortColors(self, a: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n=len(a)
        if n<=1:
            return 
        self.qs(a,0,n-1)
    def qs(self,a,l,r):
        if r<=l:
            return 
        m=self.part(a,l,r)
        self.qs(a,l,m-1)
        self.qs(a,m+1,r)
    def part(self,a,l,r):
        pivot,i=a[r],l
        for j in range(l,r):
            if a[j]<=pivot:
                a[j],a[i]=a[i],a[j]
                i+=1
        a[r],a[i]=a[i],a[r]
        return i

Go

func sortColors(a []int)  {
    n:=len(a)
    if n<=1 {
        return 
    }
    qs(a,0,n-1)
}
func qs(a []int, l,r int) {
    if r<=l {
        return 
    }
    m:=part(a,l,r)
    qs(a,l,m-1)
    qs(a,m+1,r)
}
func part(a []int, l,r int) int {
    pivot,i:=a[r],l
    for j:=l;j<r;j++ {
        if a[j]<=pivot {
            a[j],a[i]=a[i],a[j]
            i++
        }
    }
    a[i],a[r]=a[r],a[i]
    return i
}

Two pass

C++

class Solution {
public:
    void sortColors(vector<int>& a) {
        int n=a.size();
        if(n<=1)return;
        int c[2]={0,0};
        for(int i=0;i<n;i++)
            if(a[i]<=1)c[a[i]]++;
        c[1]+=c[0];
        for(int i=0;i<n;i++){
            if(i<c[0])a[i]=0;
            else if(i<c[1])a[i]=1;
            else a[i]=2;
        }
    }
};

One pass with two pointers

C++

class Solution {
public:
    void sortColors(vector<int>& a) {
        int n=a.size();
        if(n<=1)return;
        int l=0,r=n-1;
        for(int i=0;i<=r;i++){
            while(a[i]==2&&i<r)swap(a[i],a[r--]);
            while(a[i]==0&&i>l)swap(a[i],a[l++]);
        }
    }
};

Java

class Solution {
    public void sortColors(int[] a) {
        int n=a.length;
        if(n<=1)return ;
        int l=0,r=n-1;
        for(int i=0;i<=r;i++){
            while(a[i]==2&&i<r){
                int tmp=a[i];
                a[i]=a[r];
                a[r]=tmp;
                r-=1;
            }
            while(a[i]==0&&i>l){
                int tmp=a[i];
                a[i]=a[l];
                a[l]=tmp;
                l+=1;
            }
        }
    }
}

Python

class Solution:
    def sortColors(self, a: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n=len(a)
        if n<=1:
            return 
        l,r=0,n-1
        for i in range(r+1):
            if i>r:
                break
            while a[i]==2 and i<r:
                a[i],a[r]=a[r],a[i]
                r-=1
            while a[i]==0 and i>l:
                a[i],a[l]=a[l],a[i]
                l+=1
            

Go

func sortColors(a []int)  {
    n:=len(a)
    if n<=1 {
        return 
    }
    l,r:=0,n-1
    for i:=0;i<=r;i++ {
        for a[i]==2 && i<r {
            a[i],a[r]=a[r],a[i]
            r--
        }
        for a[i]==0 && i>l {
            a[i],a[l]=a[l],a[i]
            l++
        }
    }
}
posted @ 2020-08-23 10:01  winechord  阅读(135)  评论(0编辑  收藏  举报