我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/majority-element/description/
题目描述:
知识点:哈希表、分治算法、位运算、摩尔投票法
思路一:用哈希表记录各个数字出现的次数
时间复杂度和空间复杂度均是O(n)。
JAVA代码:
public class Solution {
public int majorityElement(int[] nums) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if(hashMap.containsKey(nums[i])){
hashMap.put(nums[i], hashMap.get(nums[i]) + 1);
}else{
hashMap.put(n