【LeetCode】169. Majority Element 解题小结
题目: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 解题小结相关推荐
- [勇者闯LeetCode] 169. Majority Element
[勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode - 169. Majority Element (多数投票问题)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode]: 169: Majority Element
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- [swift] LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 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 ...
- leetcode讲解--169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
最新文章
- python导入xlsxwriter要安装什么吗_Python 模块:XlsxWriter 的使用
- SSH框架整合(代码加文字解释)
- python 中cookie_Python 处理Cookie的菜鸟教程(一)Cookie库
- 刷脸支付弊端举例_刷脸支付的优势和弊端,你都知道?
- 使用PhantomJS实现网页截图服务
- 观察者模式在JDK应用中的源码分析
- JavaFX 2 GameTutorial第2部分
- Python正则表达式笔记
- 给所有开发人员的 11 条忠告(第 4 条亮了)
- vs ajax工具包引用,vs2008中使用AJAX Control Tookit工具的问题?
- SAP License:SAP顾问日记一
- centos llvm安装_CentOS7.x安装LLVM6.0
- html5背景图片幻灯片切换,如何将一个PPT的背景设置成另一个PPT的背景?
- 科普 | 金融衍生品系列——互换
- 清明祭娭毑_原水_新浪博客
- java房屋出租预约看房系统springboot ssm带房东租客
- 渗透之代理小知识--
- 使用uigi将英雄小头像显示在详细资料中(图片切换)
- Matlab中mod函数使用
- 我和宁夏日报 【白述礼】