Concise Java Bucket Sort Solution


  • 0
    P
    public void sortColors(int[] nums) {
            int[] count = new int[3];
            for (int num : nums) {
                count[num]++;
            }
            for (int i = 0; i < nums.length; i++) {
                for (int val = 0; val < count.length; val++) {
                    if (count[val]-- > 0) {
                        nums[i] = val;
                        break;
                    }
                }
            } 
        }    
    

Log in to reply
 

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