leetcode解题报告(21):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.
分析
用unordered_map来做,以元素值为key,元素个数为value。然后遍历这个map,判断有没有元素个数大于 ⌊ n/2 ⌋,如果有,就返回这个元素,否则继续遍历,直到遍历结束。
代码如下:
class Solution {
public:
int majorityElement(vector<int>& nums) {
unordered_map<int,int>m;
for(int num : nums)
++m[num];
for(auto it = m.begin(); it != m.end(); ++it){
if(it->second > nums.size() / 2)return it->first;
}
return 0;
}
};
leetcode解题报告(21):Majority Element的更多相关文章
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- 【leetcode❤python】169. Majority Element
#Method 1import math class Solution(object): def majorityElement(self, nums): numsDic={} ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- [LeetCode&Python] Problem 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 app ...
- leetcode解题报告(31):Kth Largest Element in an Array
描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...
随机推荐
- ILSVRC比赛带来的算法
李飞飞和它的团队搜集了ImageNet一个超过15 million的图像数据集,大约有22,000类.这个文件集合对深度卷积网络极大地推进深度学习各领域的发展. ILSVRC是对ImageNet进行分 ...
- Python内存加载shellcode
生成 首先生成一个测试的msf shellcode msfvenom -p windows/x64/exec CMD=calc.exe -f python 把其中的shellcode复制出来留待待会使 ...
- websocket 协议简述
WebSocket 是一种网络通信协议,RFC 6455 定义了它的通信标准,属于服务器推送技术的一种 由于 HTTP 无状态.无连接.单向通信的特性,导致 HTTP 协议无法实现服务器主动向客户端发 ...
- 使用postman mock server
需要写一个小的Java程序,用来调用云平台的接口 由于云平台的接口程序还没有写好,只能用模拟的方式先行开发代码, 用了post来模拟接口程序. 需要模拟的接口如下: ■请求地址 /openapi/ip ...
- C++ 去掉字符串的首尾空格和全部空格
#include <iostream>#include <string>using namespace std; //去掉收尾空格string& ClearHeadTa ...
- lambda的一些用法
lambda在函数中调用时可以不用传入形参,当需要时才传入参数,方便一些场合中的使用(当参数一直变化时,仍然需要调用函数,可以采用如下方式).如以下代码所示. import numpy as np d ...
- 转载 AI-Talking 图算法
https://mp.weixin.qq.com/s/2XRgJr-ydxHA3JxAZ_5HeA 图算法在风控业务的实践 直播行业中有很多业务风控问题,比如说批量注册.刷热度.垃圾信息以及薅羊毛等. ...
- ubuntu18.04 安装idea
首先从官网下载idea:IntelliJ IDEA (在安装IDEA前应先安装jdk环境) 得到ideaIU-2019.2.4.tar.gz 将安装包移动到/usr/local,这样可以让所有用 ...
- javascript_02-变量
变量 var number = 5; number = 5; //与上面一样的效果,语法没错误,但是不规范 var:关键字,变量的意思. 变量可以立即赋值,也可以稍后赋值. 堆和栈 内存中有两个区域, ...
- JAVA笔记整理(七),JAVA几个关键字
本篇主要总结JAVA中的super.this.final.static.break.continue 1.super super主要用在继承当中,表示调用父类的构造函数. 1.如果要在子类方法中调用父 ...