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 ...
随机推荐
- WPF控件模板
引言:在进行WPF项目开发过程中,由于项目的需要,经常要对某个控件进行特殊的设定,其中就牵涉到模板的相关方面的内容.本文也是在自己进行项目开发过程中遇到控件模板设定时集中搜集资料后整理出来的,以供在以 ...
- 关闭不安全的HTTP方法
关闭不安全的HTTP方法 在项目或tomcat下的web.xml中,添加如下配置: <!-- 关闭不安全的HTTP方法 --> <security-constraint> &l ...
- Android界面实现----PagerTabStrip绚丽的滑动标签
在ViewPager这种可以滑动的控件上,总是有很多的文章可以做.Android自带的控件,实现一个指示器,这个控件,就是support-v4包里面的PagerTabStrip控件. 首先,我们先看一 ...
- 堡垒机 paramiko代码
#!/usr/bin/env python # Copyright (C) - Robey Pointer <robeypointer@gmail.com> # # This file i ...
- Vue.2.0.5-混合
基础 混合是一种灵活的分布式复用 Vue 组件的方式.混合对象可以包含任意组件选项.以组件使用混合对象时,所有混合对象的选项将被混入该组件本身的选项. 例子: // 定义一个混合对象 var myMi ...
- Install .NET Framework 4.5.2 on a Cloud Service Role
October Guest OS rollout is starting today October 15 2015, and projected to be released on November ...
- tomcat 6.0.44 “has failed to stop it. This is very likely to create a memory leak” 问题调查
1. 问题起因 我们项目中缓存模块某个实现采用了ehcache(2.4.3),当项目部署到tomcat中后,对tomcat做停止服务操作(点击eclipse的console红色的停止按钮,奇怪的是有小 ...
- c++ template怎么使用及注意事项
c++ 中的template和c#的什么有点相似? 先看下定义方式: template <typename|class T> T myFunction(T param1,T param2. ...
- JMX超详细解读
一.JMX的定义 JMX(Java Management Extensions)是一个为应用程序植入管理功能的框架.JMX是一套标准的代理和服务,实际上,用户可以在任何Java应用程序中使用这些代理和 ...
- Mac下无法安装Dragon Bones的解决方案
在Mac下安装dragon bones 然后很郁闷的发现 没有 Exchange Manger 然后就去官网下载了一个 不过下载以后发现 怎么都认不出我的Flash CC来 一安装zxp扩展就提示我没 ...