[java] 0ms, derived from quick sort.


  • 0
    public void sortColors(int[] nums) {
            if (nums == null || nums.length < 2)
                return;
            //  n <= i, nums[n] == 0
            int i = -1, j = nums.length;
            int k = 0;
            
            while (k < j) {
                if (nums[k] == 1) {
                    k++;
                } else if (nums[k] == 0) {
                    nums[k++] = nums[++i];
                    nums[i] = 0;
                } else {
                    nums[k] = nums[--j];
                    nums[j] = 2;
                }
            }
            return;
        }
    

Log in to reply
 

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