【LeetCode OJ】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.
int majorityElement(vector<int> &num)
{
map<int, int> m;
int n = num.size() / ;
for (vector<int>::iterator it = num.begin(); it != num.end(); it++)
{
auto ret = m.insert(make_pair(*it, )); //对于不包含重复关键字的容器map,insert操作返回一个pair
if (!ret.second) //pair的firs成员是一个迭代器,指向具有给定关键字的元素,
++ret.first->second;//second成员是一个bool值,指出元素的插入成功还是已经存在于容器中,如果已存在,则返回bool为false }
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
if (it->second > n)
return it->first;
}
}
【LeetCode OJ】Majority Element的更多相关文章
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode 169】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode OJ】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length ...
- LeetCode OJ 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- LeetCode OJ 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ:Majority Element(主要元素)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode OJ】Pascal's Triangle II
Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...
随机推荐
- DataFrame重命名单个column
由于当时的需求我的a表和b表的公共键名称不一样 例如这个 那么我就可以进行重命名: df1 = DataFrame(np.arange().reshape((, )), columns=['a', ' ...
- SAP NUMBER RANGE维护配置object FBN1 Deletion only possible if status is initial
背景: 错误日志: SAP FBN1 Deletion only possible if status is initial 场景: 如果目标机已有NUMBER RANGE 不为0,需要删除配置年为9 ...
- 浅谈main(),int main(),void main(),int main(void)四者之间的区别
1,main():相当于 int main() 2,int main():int 是main() 函数的返回类型.这表明main()函数返回的值是整数且授受任何数量的参数. 3,void main() ...
- BIO、NIO、AIO系列二:Netty
一.概述 Netty是一个Java的开源框架.提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. Netty是一个NIO客户端,服务端框架.允许快速简 ...
- (转) 解密H264、AAC硬件解码的关键扩展数据处理
出自:http://blog.itpub.net/30168498/viewspace-1576794/ 通过上一篇文章,我们用ffmpeg分离出一个多媒体容器中的音视频数据,但是很可能这 ...
- Linux可视化服务器管理工具webmin
webmin是一个可视化的linux服务器管理工具,可以帮助我们实现很多功能. Webmin官网: http://www.webmin.com/ 下载地址:http://prdownloads.sou ...
- C# where用法解析
where 子句用于指定类型约束,这些约束可以作为泛型声明中定义的类型参数的变量.1.接口约束.例如,可以声明一个泛型类 MyGenericClass,这样,类型参数 T 就可以实现 ICompara ...
- CSS3 3D立方体翻转菜单实现教程
今天我们来看一个非常有创意的CSS3 3D菜单,这个菜单的菜单项是可以旋转的长方体,鼠标滑过是长方体即可旋转,看看下面的效果图,是不是感觉非常酷,我觉得这个菜单很适合用在咱们开发人员的个人网站上. 当 ...
- iOS:ODRefreshControl
https://github.com/Sephiroth87/ODRefreshControl Important note if your project doesn’t use ARC: you ...
- 最简单的视音频播放演示样例3:Direct3D播放YUV,RGB(通过Surface)
===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...