#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. 发送xml或json格式的数据给服务器

    后台通过context.Request.InputStream来接收 #region 发送消息 + void SendMessage() /// <summary> /// 发送消息 // ...

  2. javascript实例学习之四——javascript分页

    话不多少,直接上代码 html代码: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  3. Entity FrameWork初始化数据库的四种策略

    程序猿就是苦逼,每天还得分出一些时间去写博文.天真的很热,今天就随便写一点啦! 1.EF初始化数据库的四中策略 EF可以根据项目中的模型自动创建数据库.下面我们就分类看看Entity Framewor ...

  4. PostgreSQL auto_explain

    The auto_explain module provides a means for logging execution plans of slow statements automaticall ...

  5. PostgreSQL Replication之第十一章 使用Skytools(4)

    11.4 使用 londiste 复制数据 pgq是一个叫做londiste的复制工具的核心.londiste 的核心是有一个比如比Slony 更加简单,容易使用的机制.如果您在一个大的安装中使用Sl ...

  6. Leetcode: Insert Delete GetRandom O(1)

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  7. Smarty模板

    Smarty模板 是做什么用的?? 是将前端的显示和后台的逻辑进行分离,就相当于把前台显示的页面和后台要实现的某些功能的逻辑给分离出来了,分离在两个文件里,也就是说,前端只负责显示,后端只负责逻辑操作 ...

  8. Shiro--权限控制

    来自: http://m.oschina.net/blog/92003 1.applicationContext-shiro.xml配置:实现认证和授权 <!-- shiro start --& ...

  9. VS2012窗口及编辑文本框背景颜色变黑

    1.工具->选项 2.环境->常规->深色

  10. C#删除xml中某个节点的子节点方法

    if (File.Exists(xmlFilePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); Xm ...