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++
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值