Count Sort


  • 0
    W
    public void sortColors(int[] nums) {
        int[] arr = new int[3];
        
        for(int num: nums){
            arr[num]++;
        }
        
        int k = 0;
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i]; j++){
                nums[k++] = i;
            }
        }
    }
    

Log in to reply
 

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