My Solution for this Question


  • 1
    B
    public void sortColors(int[] nums) {
    		int len = nums.length;
    		int reds=0;
    		int whites=0;
    		int last = len-1;
    		for(int i=len-1;i>=0;i--){
    			if (nums[i]==2) {
    				nums[last--]=2;
    			}
    			if (nums[i]==1) {
    				whites++;
    			}
    		}
    		reds = last-whites;
    		for(int i=0;i<=reds;i++){
    			nums[i]=0;
    		}
    		for(int i=reds+1;i<=last;i++){
    			nums[i] = 1;
    		}
    		
    	}

Log in to reply
 

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