题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Credits:

自己的思路大概是建立一个map,key对应数字,value对应数字出现的次数,然后比较次数最多的那个应该就是Majority Element。

class Solution {
public:unordered_map<int, int> map;int majorityElement(vector<int>& nums) {int indexMax;for (int i = 0; i < nums.size(); i++) {if (map.find(nums[i]) == map.end()) {map[nums[i]] = 1;}else {map[nums[i]]++;}}indexMax = 0;for (int i = 1; i < nums.size(); i++) {if (map[nums[i]] > map[nums[indexMax]] && map[nums[i]] >= map.size() / 2)indexMax = i;}return nums[indexMax];}
};

转载于:https://www.cnblogs.com/Doctengineer/p/5798955.html

【LeetCode】169. Majority Element 解题小结相关推荐

  1. [勇者闯LeetCode] 169. Majority Element

    [勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...

  2. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. leetcode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. Leetcode - 169. Majority Element (多数投票问题)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. [LeetCode]: 169: Majority Element

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  6. [swift] LeetCode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. Leetcode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. LeetCode 169 Majority Element(Majority Voting Algorithm)

    Majority Element Given an array of size n, find the majority element. The majority element is the el ...

  9. leetcode讲解--169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

最新文章

  1. python导入xlsxwriter要安装什么吗_Python 模块:XlsxWriter 的使用
  2. SSH框架整合(代码加文字解释)
  3. python 中cookie_Python 处理Cookie的菜鸟教程(一)Cookie库
  4. 刷脸支付弊端举例_刷脸支付的优势和弊端,你都知道?
  5. 使用PhantomJS实现网页截图服务
  6. 观察者模式在JDK应用中的源码分析
  7. JavaFX 2 GameTutorial第2部分
  8. Python正则表达式笔记
  9. 给所有开发人员的 11 条忠告(第 4 条亮了)
  10. vs ajax工具包引用,vs2008中使用AJAX Control Tookit工具的问题?
  11. SAP License:SAP顾问日记一
  12. centos llvm安装_CentOS7.x安装LLVM6.0
  13. html5背景图片幻灯片切换,如何将一个PPT的背景设置成另一个PPT的背景?
  14. 科普 | 金融衍生品系列——互换
  15. 清明祭娭毑_原水_新浪博客
  16. java房屋出租预约看房系统springboot ssm带房东租客
  17. 渗透之代理小知识--
  18. 使用uigi将英雄小头像显示在详细资料中(图片切换)
  19. Matlab中mod函数使用
  20. 我和宁夏日报 【白述礼】

热门文章

  1. python对象的复制问题
  2. 还是畅通工程1233
  3. header.vue 调用变量,别的组件导入引用,组件方法事例实例
  4. PLSQL Developer学习笔记
  5. 编辑器Ultraedit快捷键
  6. SpringMVC @Transactional注解方式事务失效的解决办法
  7. 2016集训测试赛(二十六)Problem A: bar
  8. SAP如何自定义客户编码
  9. JS eval函数作用
  10. Nodejs 英雄管理系统