rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

229. 多数元素 II #101

Open rocksc30 opened 1 year ago

rocksc30 commented 1 year ago
class Solution {
    public List<Integer> majorityElement(int[] nums) {
        Set<Integer> ans = new HashSet<>();
        int len = nums.length;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
            if (map.get(nums[i]) > len / 3)
                ans.add(nums[i]);
        }
        return new ArrayList<>(ans);
    }
}
rocksc30 commented 1 year ago

简单的中等题