题目: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. JUnit断言

    在本节中,我们将介绍一些断言方法.所有这些方法都受到 Assert 类扩展了java.lang.Object类并为它们提供编写测试,以便检测故障.下表中有一种最常用的断言方法的更详细的解释. 断言 描 ...

  2. 【未通过】LintCode #366 斐波纳契数列

    实现: public class Solution { /** * @param n: an integer * @return: an ineger f(n) */ public int fibon ...

  3. 嵌入式开发之zynq驱动—— zynq ps pl ddr 内存地址空间映射

    http://www.wiki.xilinx.com/Zynq-7000+AP+SoC+-+32+Bit+DDR+Access+with+ECC+Tech+Tip http://patchwork.o ...

  4. VMWare中Linux虚拟机设置静态IP上网的设置方法

    VMWare中Linux虚拟机设置静态IP上网的设置方法 标签: vmwareLinux虚拟机securecrt静态IP上网 2016-05-18 02:30 702人阅读 评论(0) 收藏 举报   ...

  5. Python WSGI开发随笔

    本文记录学习WSGI时的一些知识点,值得后续学习中注意. wsgi应用接口只要是一个可调用的对象就行,这个可调用的对象需要: 1. 接受两个位置参数: a. 包含CGI形式变量的字典: b. 应用调用 ...

  6. js 四级联动

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  7. Windows下安装Oracle12C(二)

    一.安装Oracle 参考 Windows下安装Oracle12C(一) 二.新建用户 1. 安装完成后,启动SQLPlus,然后输入内置的用户名和密码 用户名:sys   密码:as sysdba ...

  8. 【玩转Golang】 自定义json序列化对象时,非法字符错误原因

    由于前台web页面传来的日期对象是这样的格式“2010-11-03 15:23:22”,所以我安装网上查来的办法,自定义包装了time.Time对象,实现自己的Marshal和UnMarshal方法 ...

  9. vue实现点击关注之后及时更新列表

    如图,我要实现点击关注之后列表及时更新成最新的列表. 思路很简单,主要是两点: 1.在点击关注之后去执行一个请求新的关注列表的action: 2.在vue组件中watch监听已关注列表和推荐关注列表 ...

  10. debian、ubuntu:使用apt包管理器可能存在的问题! 让新手望而却步!

    apt包管理器说好真好,说不好真不好. 最近在debian9.ubuntu18.04上安装oracle 10g 玩. 怎么都准备不好安装环境.原因就是i386构架体系的deb包总安装不正确! baid ...