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 ...
随机推荐
- 图像分割之(二)Graph Cut(图割)
zouxy09@qq.com http://blog.csdn.net/zouxy09 上一文对主要的分割方法做了一个概述.那下面我们对其中几个比较感兴趣的算法做个学习.下面主要是Graph Cut, ...
- SQLServer 统计数据量
做一个项目,第一件事情就是问:“这个数据库多大?” 下面是统计数据库数据量大小的方法 通常我们会使用命令: "sp_helpdb @dbname" 例如,查询数据库"te ...
- 利用DBCC PAGE查看SQL Server中的表和索引数据
16:08 2013-01-06 参考http://doc.chinaunix.net/sqlserver/200810/206034.shtmlDBCC PAGE|IND 参数 DBCC PAGE ...
- java 笔记(5) —— 线程,yield,join
一.线程各个状态与转换: 新建状态:用new语句创建的线程对象处于新建状态,此时它和其它的java对象一样,仅仅在堆中被分配了内存 .就绪状态:当一个线程创建了以后,其他的线程调用了它的start() ...
- CRM 迁移服务器备忘
1. 安装IIS 2. 安装sqlexpress 2005, sql management studio express 3. 安装.net framework4.0 4. 安装filezilla S ...
- C++Primer 第十三章
//1.当定义一个类时,我们显示地或隐式地指出在此类型的对象(注意这里是此类型的对象,而不包括此类型的指针)拷贝,移动,赋值,销毁时做什么.一个类通过定义五种特殊的成员函数来控制这些操作:拷贝构造函数 ...
- 去掉字符串中的空格 JS JQ 正则三种不同写法
<script> function trim(str) { return str.replace(/(^\s*|\s*$)/g, "") } console.log(t ...
- C#控件:TabControl
tabcontrol在切换页面的时候经常用到 这里讲下如何让tabcontrol更好看 ref:http://blog.csdn.net/conmajia/article/details/759671 ...
- bean中集合属性的配置
在实际的开发中,有的bean中会有集合属性,如下: package com.sevenhu.domain; import java.util.List; /** * Created by hu on ...
- php获取文件后缀名格式
function get_extension($file) { substr(strrchr($file, '.'), 1); } 第2种方法: function get_extension($fil ...