Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 693 Bytes

leetcode-75.md

File metadata and controls

33 lines (26 loc) · 693 Bytes

Question

link

Solution

Brute force approach

class Solution {
    public void sortColors(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();

        for(int i=0; i< nums.length; i++){
            if (map.containsKey(nums[i])) {
                map.put(nums[i], map.get(nums[i])+1);
            }
            else{
                map.put(nums[i], 1);
            }
        }
        int idx = 0;

        for (Map.Entry<Integer, Integer> e : map.entrySet()){

            for(int j=0; j< e.getValue(); j++){
                nums[idx] = e.getKey();
                idx++;
            }
        }
    }
}