JAVA solution __ look for your opinions


  • -2
    H

    public class Solution {
    public void sortColors(int[] nums) {
    int oneCount = 0,twoCount=0;
    int j =0;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==0){
    nums[j++]=0;
    }
    else if(nums[i]==1){
    oneCount++;
    }
    else{
    twoCount++;
    }
    }
    while(oneCount>0){
    nums[j++]=1;
    oneCount--;
    }
    while(twoCount>0){
    nums[j++]=2;
    twoCount--;
    }

    }
    

    }


Log in to reply
 

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