多数元素(simple难度)

https://leetcode-cn.com/problems/majority-element/

与本题相同题目:

剑指offer39. 数组中出现次数超过一半的数字

本文思路及解法参考了《剑指offer39.数组中出现次数超过一半的数字》的题解
作者:jyd
链接:https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/solution/mian-shi-ti-39-shu-zu-zhong-chu-xian-ci-shu-chao-3/
来源:力扣(LeetCode)

class Solution {public int majorityElement(int[] nums) {int x = 0, votes = 0;for(int num : nums){if(votes == 0) x = num;votes += num == x ? 1 : -1;}return x;}
}作者:jyd
链接:https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/solution/mian-shi-ti-39-shu-zu-zhong-chu-xian-ci-shu-chao-3/
来源:力扣(LeetCode)

class Solution {public int majorityElement(int[] nums) {int x = 0, votes = 0, count = 0;for(int num : nums){if(votes == 0) x = num;votes += num == x ? 1 : -1;}// 验证 x 是否为众数for(int num : nums)if(num == x) count++;return count > nums.length / 2 ? x : 0; // 当无众数时返回 0}
}作者:jyd
链接:https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/solution/mian-shi-ti-39-shu-zu-zhong-chu-xian-ci-shu-chao-3/
来源:力扣(LeetCode)

哈希表法:

class Solution {private Map<Integer, Integer> countNums(int[] nums) {Map<Integer, Integer> counts = new HashMap<Integer, Integer>();for (int num : nums) {if (!counts.containsKey(num)) {counts.put(num, 1);} else {counts.put(num, counts.get(num) + 1);}}return counts;}public int majorityElement(int[] nums) {Map<Integer, Integer> counts = countNums(nums);Map.Entry<Integer, Integer> majorityEntry = null;for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {if (majorityEntry == null || entry.getValue() > majorityEntry.getValue()) {majorityEntry = entry;}}return majorityEntry.getKey();}
}作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/majority-element/solution/duo-shu-yuan-su-by-leetcode-solution/
来源:力扣(LeetCode)

排序法:

class Solution {public int majorityElement(int[] nums) {Arrays.sort(nums);return nums[nums.length / 2];}
}作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/majority-element/solution/duo-shu-yuan-su-by-leetcode-solution/
来源:力扣(LeetCode)

LeetCode热题100使用摩尔投票法的题目整理(待更)相关推荐

  1. LeetCode 热题100答案

    leetcode 热题100 文章目录 leetcode 热题100 [2. 两数相加](https://leetcode-cn.com/problems/add-two-numbers/) [3. ...

  2. 【leetcode热题100】刷题记录(5-8)

    [leetcode热题100]刷题记录(5-8) 5. 最长回文子串 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个正序数组的中位数 5. 最长回文子串 题目链接 给你一个字符串 s,找到 ...

  3. LeetCode热题100中使用辅助栈方法的题目的整理(待更)

    有效的括号(simple难度) https://leetcode-cn.com/problems/valid-parentheses/ 本文思路及代码来源 作者:jyd 链接:https://leet ...

  4. LeetCode热题100道

    文章目录 1.两数之和 2.两数相加 3.无重复字符的最长子串 4.最长回文子串 5.盛最多水的容器 6.删除链表的倒数第 N 个结点 7.Day 2 [未完待续] 1.两数之和 通往[LeetCod ...

  5. C语言刷题之摩尔投票法

    目录 1.引入 2.摩尔投票算法 3.基本步骤 摩尔投票法分为两个阶段: 1.抵消阶段 2.检验阶段 4.代码实现 5.扩展沿伸 6.总结 1.引入 我们来看一个问题: 假设有一个无序数组长度为n,要 ...

  6. [剑指offer][JAVA]面试题第[39]题[数组中出现次数超过一半的数字][HashMap][摩尔投票法]

    [问题描述][简单] 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.你可以假设数组是非空的,并且给定的数组总是存在多数元素.示例 1:输入: [1, 2, 3, 2, 2, 2, 5, ...

  7. LeetCode 热题 HOT 100

    LeetCode 热题 HOT 100之JAVA解法 第1题 两数之和(简单) 代码 第2题 两数相加(中等) 代码 第3题 无重复字符的最长子串(中等) 代码 第4题 寻找两个正序数组的中位数(困难 ...

  8. 《LeetCode 热题 HOT 100》Java答案汇总版---持续更新中

    <LeetCode 热题 HOT 100>Java答案汇总版-持续更新中 个人认为<LeetCode 热题 HOT 100>中的题目特别适合算法新手进行一个入门的刷题,而且作者 ...

  9. 【LeetCode笔记】169. 多数元素(Java、摩尔投票法、哈希表)

    文章目录 题目描述 思路 & 代码 思路一:哈希表 思路二: 摩尔投票法 题目描述 好家伙,这是今天最有意思的题目了 思路 & 代码 思路一:哈希表 先说缺点:空间复杂度O(n) 一次 ...

  10. 动态规划和摩尔投票法

    动态规划 维基百科对动态规划(Dynamic programming,简称DP)的定义是一种在数学.管理科学.计算机科学.经济学和生物信息学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问 ...

最新文章

  1. 来自顶尖JAVA程序猿的焦虑,拒绝中年危机,唯有一生力学笃行
  2. 002_jQuery语法
  3. 用电脑发短信_丢掉你的手机数据线吧!用这个软件轻松在任何设备互传文件
  4. hadoop(一) 基本介绍
  5. Beautifulsoup的使用
  6. python开发工具管理系统_Python之软件管理
  7. suse linux下交叉编译,阐述SUSE 10.1交叉编译环境构建方法
  8. mysql all语法怎么用_MySQL UNION 与 UNION ALL 语法与用法
  9. UVa 11468 (AC自动机 概率DP) Substring
  10. paypal创建订单后怎么获得id_PayPal开放加密数字货币买卖 2021年将支持2600万家商户购物...
  11. [技巧心得] 嵌套选择器优先级
  12. Cassandra数据库模糊查询
  13. 《精通linux设备驱动程序开发》——USB
  14. java string 返回方法_Java语言中,String类的IndexOf()方法返回的类型是()_学小易找答案...
  15. (转)驾驶证到期换证指南
  16. 【激光原理】多普勒烧孔原理的三维可视化图像绘制
  17. upload-labs通关详解
  18. dbNSFP:非同义突变功能注释数据库
  19. ”好奇号“在火星表面漫游的证据
  20. Android微信智能心跳方案 (转)

热门文章

  1. 【## 1、浏览器及内核-】
  2. c语言ifi=1 2,第1部分_C语言程序设计练习一全解.doc
  3. Comsol With Matlab启动失败解决
  4. 网络设备常见接口和模块
  5. 快速开发工具的两种实现模式
  6. 浅论汽车电子行业的汽车开放系统架构AUTOSAR
  7. 从k-line到决策实践:huobi api实现(一)
  8. Hadoop经典书籍资料收藏(35本)转
  9. 关于修复msvcp110.dll丢失的问题
  10. NAS网络存储器(转)