解题思路:

使用一个map

提交代码:

class Solution {
    public int majorityElement(int[] nums) {
    	int len=nums.length;
    	Map<Integer,Integer> map=new HashMap<>();
        for(Integer num :nums) {
        	if(map.containsKey(num)) {
        		map.put(num,map.get(num)+1);
        	}else {
        		map.put(num,1);
        	}
        	if(map.get(num)>len/2)	return num;
        }
        return nums[0];
    }
}

运行结果:
【leetcode】169(Easy). Majority Element

相关文章:

  • 2021-08-12
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
猜你喜欢
  • 2021-07-07
  • 2021-05-29
  • 2021-09-12
  • 2021-05-17
  • 2021-10-27
  • 2021-07-16
  • 2021-11-05
相关资源
相似解决方案