【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 ...
随机推荐
- android 拍照声音文件路径
Android拍照音频文件位于\frameworks\base\data\sounds\effects目录,更具不同的平台区分不同音频文件. 例如拍照声音文件位于\frameworks\base\da ...
- Android Error: This attribute must be localized.
在android中使用mmm命令编译程序是出现错误. 这种问题一般情况是因为在res/xml文件夹下的中, 或者在res/layout下的文件中出现了没有多语言话的文本例. 解决方法: 不直接在布局文 ...
- JDK源码阅读之Collection
源码版本:JDK 1.7. 集合 Collection,根据已知的内容可以知道有List.Set.Map(严格说,Map不属于Collection)等大类. 先查看 Collection, publi ...
- c# WebBrowser获取cookie
private void BtnOpenUrl_Click(object sender, EventArgs e) { if (txtUrl.Text != "") { Myweb ...
- Spring Boot 快速搭建的三种方式
方式一:http://start.spring.io/ 打开浏览器,在地址栏中输入http://start.spring.io/ 如下图: 点击generate project 然后就会有一个zip ...
- Aspose.Words对于Word的操作
对于word操作一般是对已有word模板的操作,直接新建的不考虑,网上教程很多,自己看吧一般有以下几种办法(忘了具体几种了,一般情况下以下就够了)1.通过书签替换顾名思义,就是先定义一个书签,然后在书 ...
- R语言curve绘图函数
curve 函数常用于绘制函数对应的曲线,确定函数的表达式,以及对应的需要展示的起始坐标和终止坐标,curve函数就会自动化的绘制在该区间内的函数图像 基本用法,代码示例: curve(sin, -2 ...
- T4生成多文件时,不生成自己
如:我用的网上的生成多文件的一个include文件. 生成多文件时,默认会生成一个以自己名字命名的文件如: 有一个demo.tt文件,生成时会出来一个demo.cs文件(默认情况下) 解决方法: Fo ...
- 第一個shell腳本
#!/bin/bash echo "Hello World !" 運行
- python实现原地刷新方式输出-可用于百分比进度显示输出
方式1: import sys sys.stdout.write('\r' + '你的输出详情') sys.stdout.flush() 方式2: print('\r' + '你的输出详情', end ...