A simple solution with c++ 5ms


  • 0
    1
    class Solution {
    public:
        void swap(int &a,int &b)
        {
            int temp=b;
            b=a;
            a=temp;
        }
        void sortColors(int A[], int n) {
            if(A==NULL||n==0)
               return;
            int start=0,end=n-1;
            for(int i=0;i<=end;i++)
            {
                while(A[i]==2&&i<=end)
                {
                    swap(A[i],A[end]);
                    end--;
                }
                if(A[i]==0)
                {
                    swap(A[i],A[start]);
                    start++;
                }
               
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.