描述

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.

分析

unordered_map来做,以元素值为key,元素个数为value。然后遍历这个map,判断有没有元素个数大于 ⌊ n/2 ⌋,如果有,就返回这个元素,否则继续遍历,直到遍历结束。

代码如下:

class Solution {
public:
int majorityElement(vector<int>& nums) {
unordered_map<int,int>m;
for(int num : nums)
++m[num];
for(auto it = m.begin(); it != m.end(); ++it){
if(it->second > nums.size() / 2)return it->first;
}
return 0;
}
};

leetcode解题报告(21):Majority Element的更多相关文章

  1. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  2. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  5. 【leetcode❤python】169. Majority Element

    #Method 1import math class Solution(object):    def majorityElement(self, nums):        numsDic={}   ...

  6. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  7. [LeetCode&Python] Problem 169. Majority Element

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

  8. LeetCode(169)Majority Element

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

  9. leetcode解题报告(31):Kth Largest Element in an Array

    描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

随机推荐

  1. shiro与spring集成

    简介 Apache Shiro 是 Java 的一个安全(权限)框架.主要提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份:Auth ...

  2. 题解-CSA Beta Round#1 Number Elimination

    Problem CSA-Beta Round#3 题意概要:给定 \(n\) 个数组成的序列,定义一次操作: 在当前序列中选择两个数,将其中较小的数从序列中删除(若两个数相同,则删除在序列中更靠前的) ...

  3. java 线程实现、线程暂停和终止 、线程联合join、线程基本信息获取和设置、线程优先级

    转载地址:速学堂 https://www.sxt.cn/Java_jQuery_in_action/eleven-inheritthread.html 1. 通过继承Thread类实现多线程 继承Th ...

  4. @objc

    Swift 和 Objective-C 的互调这个话题很大,今天我们重点看看其中一个小的知识点:@objc的使用. 用法 在 Swift 代码中,使用@objc修饰后的类型,可以直接供 Objecti ...

  5. vue的data里面的值是数组时,在更改其某一项的时候,怎么触发视图的重新渲染?

    1. 设置对象或数组的值:Vue.set(target,key,value) :2.删除对象或数组中元素: Vue.delete ( target,key) ;3. 数组对象直接修改属性,可以触发视图 ...

  6. 远程 Linux(Ubuntu 18)添加字体

    安装 xshell与xftp 连接xshell 点击 xshell上方工具栏中的xftp图标, 自动连接xftp linux下创建字体目录 su cd / cd usr/share/fonts mkd ...

  7. map函数的应用

    map函数在Python中的应用 函数介绍: map() 会根据提供的函数对指定序列做映射. map(function, iterable, ...) 第一个参数function 以参数序列中的每一个 ...

  8. 在iframe内页触发顶层页面body的blur事件

    //在iframe内页触发顶层页面body的blur事件. if (window != top) { $(document.body).click(function () { $(top.docume ...

  9. 阿里云服务器(Ubuntu16.04 64位)的使用

    购买阿里云服务器 1.打开阿里云官方网站,账号登录,选择产品中的云服务器 ECS 2.根据自身需求,选择合适的阿里云服务器系统,(1)点击一键购买,(2)选择地域,(3)根据自身需求,选择系统,这里选 ...

  10. Redis其他数据结构

    用户日活月活怎么统计 - Redis HyperLogLog 详解 HyperLogLog 提出问题 我们先思考一个常见的业务问题:如果你负责开发维护一个大型的网站,有一天老板找产品经理要网站每个网页 ...