题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

 int majorityElement(vector<int> &num)
{
map<int, int> m;
int n = num.size() / ;
for (vector<int>::iterator it = num.begin(); it != num.end(); it++)
{
auto ret = m.insert(make_pair(*it, )); //对于不包含重复关键字的容器map,insert操作返回一个pair
if (!ret.second) //pair的firs成员是一个迭代器,指向具有给定关键字的元素,
++ret.first->second;//second成员是一个bool值,指出元素的插入成功还是已经存在于容器中,如果已存在,则返回bool为false }
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
if (it->second > n)
return it->first;
}
}

【LeetCode OJ】Majority Element的更多相关文章

  1. 【LeetCode 229】Majority Element II

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

  2. 【LeetCode 169】Majority Element

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

  3. 【LeetCode OJ】Remove Element

    题目:Given an array and a value, remove all instances of that value in place and return the new length ...

  4. LeetCode OJ 229. Majority Element II

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

  5. LeetCode OJ 169. Majority Element

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

  6. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  7. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  8. LeetCode OJ:Majority Element(主要元素)

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

  9. 【LeetCode OJ】Pascal's Triangle II

    Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...

随机推荐

  1. 年中总结大会--总结学习笔记, 技术部IT部门年中总结报告该怎么写

    组织年中总结大会该如何组织 1. 通知每一位员工, 总结自己的半年工作(提前多少天, 截至日期) 2. 会场布置 3. 部门领导总结 4. 中场休息, 或节目表演, 合影等 5. 表彰 6. 交流(提 ...

  2. AngularJS $http模块POST数据,后台接受不到

    1.问题: 后端接收不到AngularJs中$http.post发送的数据,总是显示为null 示例代码: $http.post(/admin/KeyValue/GetListByPage, { pa ...

  3. PyQt的图片资源的路径问题。

    百度到的PyQt的添加资源大部分都是通过Qt Creater添加资源的,适用于拖拽形成的界面. 问题一:纯粹手写的界面,添加资源呢? 文件夹路径: |----img |--aa.jpg |----vi ...

  4. Python资料收藏(杂乱版)

    http://blog.csdn.net/vagrxie/article/category/343814 http://blog.sina.com.cn/s/articlelist_280149524 ...

  5. ie10以上媒体查询 css

    @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { }

  6. 本地没问题 服务器 提示 Server Error in '/' Application

    一. 先用本机的 IIS 测试,不要用 VS 内附的 Web server,並配置 <customErrors mode="Off"/>,以將真實的錯誤原因顯示出來,看 ...

  7. JSP内置对象—session

    什么是session? session对象是用来在每个用户之间分别保存每个用户信息的对象,以便跟踪用户的操作状态.session的信息保存在server端,session的id保存在client的co ...

  8. 使用DataSource绑定一维数组时,DataTextField只需绑定空字符串

    方法定义: public static void InitDropDownList(DropDownList ddl, bool isAddTopItem, DropDownList ddlSub, ...

  9. Two FIFOs of length 253 with 8-bits

    FIFO 先入先出队列(First Input First Output,FIFO) 可以实现数据缓存. 一.FIFO的一些重要参数: 1.length:未知,待查 //补充:学长说:“FIFO一般只 ...

  10. linux环境变量的概述

    https://blog.csdn.net/u010533843/article/details/54986646 https://www.linuxidc.com/Linux/2017-08/146 ...