Java 0ms One Pass


  • 0
    public void sortColors(int[] nums) {
            int r=nums.length-1, l=0;
            for (int i=0; i<=r; i++) {
                while (nums[i]==2 &&i<r) {
                    int p=nums[i];
                    nums[i]=nums[r];
                    nums[r--]=p;
                }
                if (nums[i]==0 ) {
                    int p=nums[i]; 
                    nums[i]=nums[l];
                    nums[l++]=p;
                }
            }
        }
    

Log in to reply
 

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