Majority Element
#include<map>
using namespace std;
class Solution {
public:
int majorityElement(vector<int>& nums) {
map<int,int> m;
int n=nums.size();
int i=0;
while(i<nums.size()){
if(m.count(nums[i])) m[nums[i]]++;
else m.insert(pair<int,int>(nums[i],1));
i++;
}
i=0;
map <int, int>::iterator m1_Iter;
for ( m1_Iter = m.begin( ); m1_Iter != m.end( ); m1_Iter++ ){
if(m1_Iter->second>n/2)
return m1_Iter->first;
}
}
};
Majority Element的更多相关文章
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- (Array)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 ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
- Leetcode # 169, 229 Majority Element I and II
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 ...
- 【10_169】Majority Element
今天遇到的题都挺难的,不容易有会做的. 下面是代码,等明天看看Discuss里面有没有简单的方法~ Majority Element My Submissions Question Total Acc ...
随机推荐
- JavaScript权威指南(第六版)--JavaScript概述 DEMO
//所有在双斜线之后的内容都属于注释 //仔细阅读这里的注释:它们对JavaScript代码做了解释 //变量是表示值的一个符号名字 //变量是通过var关键字声明的 var x; //声明一个变量x ...
- [转载]ArcGIS Engine 中的多线程使用
ArcGIS Engine 中的多线程使用 原文链接 http://anshien.blog.163.com/blog/static/169966308201082441114173/ 一直都想写 ...
- hadoop NameNode HA 和ResouceManager HA
官网配置地址: HDFS HA : http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/HDFSHighAvai ...
- 【未解决】CImage::Save / Load 导致“线程 0xc224 已退出,返回值为 1 (0x1)”
不知道这个返回值意味着什么,反正只要用到Save/Load就会出现这个情况. 这个链接:http://forums.codeguru.com/showthread.php?354017-The-thr ...
- log4j2.x 配置文件默认寻找顺序
Automatic Configuration Log4j has the ability to automatically configure itself during initializatio ...
- C++Primer 第十六章
//1.模板定义以关键字template开始,后跟一个模板参数列表,此列表不能为空.编译器用推断出的模板参数来实例化一个特定版本的函数.类型参数前必须使用class或者typename(推荐使用typ ...
- Swift游戏实战-跑酷熊猫 03 熊猫跑动动画
这节内容,我们一起来利用SKAction的来为熊猫创建动画,我们将学会通过纹理组产生动画,使用永远循环的SKAction让熊猫不停的跑动. 要点: 枚举的使用:用来记录熊猫的动作状态 enum Sta ...
- 机器学习笔记:Gradient Descent
机器学习笔记:Gradient Descent http://www.cnblogs.com/uchihaitachi/archive/2012/08/16/2642720.html
- log4net面面观之工作原理
转自:逗逼的博客:http://itrust.cnblogs.com/archive/2005/01/25/97225.html 要知道Log4net究竟是咋干活的,咱们可以从下面这个脉络简图入手.你 ...
- jsp eclipse 创建jsp项目
选择File菜单 File->new->Other... 选择Dynamic Web Project项目->Next 写入项目->Finish 在WebContent鼠标右键- ...