1 ms java solution


  • 0
    S
    public void sortColors(int[] nums) {
            int[] colors = new int[3];
            for(int i = 0 ; i < nums.length ; i++){
                colors[nums[i]]++;
            }
            int j = 0;
            int cur = 0;
            for(int i = 0 ; i < colors.length ; i++){
                cur = j;
                while(colors[i] != 0 && j < cur+colors[i]){
                    nums[j++] = i;
                }
            }
        }
    

Log in to reply
 

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