Share my 0ms java solution by two pointers


  • 0
    C
    public void sortColors(int[] nums) {
        for(int redIndex = 0, blueIndex = nums.length-1, i=redIndex; i<=blueIndex;){
        	if(nums[i]==0) swap(nums, i++, redIndex++);
        	else if(nums[i] == 2) swap(nums, i, blueIndex--);
        	else i++;
        }
    }    
    private void swap(int[] array, int m, int n){
    	int temp = array[m];
    	array[m] = array[n];
    	array[n] = temp;
    }

Log in to reply
 

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