JAVA O(n) simple solution


  • 0
    O
     int[]num=new int[3];
        for(int i=0;i<nums.length;i++){
            num[nums[i]]++;
        }
        int len=0;
        for(int i=0;i<3;i++){
            for(int j=0;j<num[i];j++){
                nums[len++]=i;
            }
        }
    

    Because of there only have 3 colors,so we can use an array of sizes of 3 to store the member of occurrences times of different colors.Then insert colors to nums.


Log in to reply
 

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