#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的更多相关文章

  1. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  2. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. 【leetcode】Majority Element

    题目概述: Given an array of size n, find the majority element. The majority element is the element that ...

  4. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. (Array)169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. LeetCode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

  8. 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 ...

  9. LeetCode【169. Majority Element】

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. 【10_169】Majority Element

    今天遇到的题都挺难的,不容易有会做的. 下面是代码,等明天看看Discuss里面有没有简单的方法~ Majority Element My Submissions Question Total Acc ...

随机推荐

  1. 30天,APP创业从0到1【7.25上海站】

    活动概况 时间:2015年7月25日13:30-16:30 地点:太库•上海孵化器(张江金科路2889号长泰广场c座12楼) 主办:APICloud.诸葛.圣诺资讯 联合主办:微链.太库•上海孵化器 ...

  2. yum报错: Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

    在Centos 5.x或6.x上安装RHEL EPEL Repo repository,资源库,源的意思.RHEL EPEL(Extra Packages for Enterprise Linux)  ...

  3. 第九篇 SQL Server安全透明数据加密

    本篇文章是SQL Server安全系列的第九篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...

  4. oracle 常用视图和表

    1.查看当前用户的基本信息 select * from user_users 2.查看当前用户的角色 select * from user_role_privs 3.查看当前用户的系统权限和表权限 s ...

  5. [转] [环境搭建] VS-Visual Studio-IIS Express 支持局域网访问

    ------------------------------------------- 更正, 我发现Program一项只设置为iisexpress.exe是不可行的. 只能设置为Any才行(当然这是 ...

  6. nssstring 转换大小写

    //7.将字符串中的字母转换成大写 NSString * str10 = @"china"; NSString * tmpStr1 = [str10 uppercaseString ...

  7. expect实现交互式脚本

    #!/usr/bin/expect -f ##告诉解释器用expect来解释 set timeout 6 ##设置超时时间 ] ## 这个是传递给脚本的第一个参数,并把参数赋值给user ] ## 这 ...

  8. 11G中自动收集统计信息

    在11G中,引入了一个名为 gather_stats_prog 的自动运行任务专用于自动收集统计信息.其对应的客户端名称为"auto optimizer stats collection&q ...

  9. NSDictionary

    // ------------------------------字典------------------------------------------------- NSDictionary *d ...

  10. cache与SDRAM

    hugohong hugohong 本版等级:   #2 得分:20回复于: 2009-04-19 21:51:03 牛人说的,拿出来分享一下:cache是高速缓冲, 解决高速cpu和相对低速sdra ...