【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 ...
随机推荐
- 模拟ajax实现网络爬虫——HtmlUnit
最近在用Jsoup抓取某网站数据,可有些页面是ajax请求动态生成的,去群里问了一下,大神说模拟ajax请求即可.去网上搜索了一下,发现了这篇文章,拿过来先用着试试. 转帖如下: 网上关 ...
- (笔记)Mysql实例:建库建表并插入数据2
drop database if exists school; // 如果存在SCHOOL则删除create database school; // 建立库SCHOOLuse school; / ...
- 使用xshell5 从CentOS主机download资料
需要安装一个软件,步骤如下: 第一步,找出软件的完整名称: 第二步,安装该软件: 第三步,执行sz命令. 参考下面的执行过程 [root@mylinux ~]# yum provides lrzsz* ...
- VIM下的普通模式的相关知识
什么为一次操作? 从进行插入模式开始,直到返回普通模式为止,在此期间的任何修改都视为一次操作: 使用 u 可以撤销最新的修改: 所以呢,控制好在插入模式的操作就可以控制好撤销命令的粒度: 另外,最 ...
- 嵌入式开发之hi3519---spi nor flash启动
author:pkf qq:1327706646 1.官方hi3519默认是硬件3byte 地址模式,配置完ddr始终后,sdkv100.020是可以支持正常启动,用到低位16Mflash内存 2.如 ...
- (转) s-video vs. composite video vs. component video 几种视频格式详细说明和比较
之前对着几种视频格式认识不是很清晰,所以看数据手册的时候,看的也是稀里糊涂的. 因为项目中需要用到cvbs做视频输入,在元器件选型上,看到tw2867的数据手册上,有这么一句话: The TW2867 ...
- JsonObject和Gson详解
参考文件:http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html一.JsonObject 1.JAR包简介 要使程序可以运行必 ...
- C# 把string字符导出到txt文档方法
public static string writtxt(string html, string file) { FileStream fileStream = new FileStream(Envi ...
- phd文献阅读日志-博一上学期
为了记住并提醒自己阅读文献,进行了记录(这些论文都是我看过理解的),论文一直在更新中. 博一上学期: 1.week 6,2017.10.16 2014-Automatic Semantic Model ...
- centos6.5 手动安装gcc
gcc版本:gcc-4.4.7 rpm -Uvh mpfr-2.4.1-6.el6.x86_64.rpm rpm -Uvh cpp-4.4.7-16.el6.x86_64.rpmrpm -Uvh ...