169. Majority Element - LeetCode
Question

Solution
思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字.
还有一种思路是:对这个数组排序,次数超过n/2的元素必然在中间.
Java实现:
public int majorityElement(int[] nums) {
Map<Integer, Integer> countMap = new HashMap<>();
for (int num : nums) {
Integer count = countMap.get(num);
if (count == null) {
count = 0;
}
countMap.put(num, count+1);
}
for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
if (entry.getValue() > nums.length/2) {
return entry.getKey();
}
}
return 0;
}
在讨论区看到一个创新的解法:
public int majorityElement(int[] num) {
int major=num[0], count = 1;
for(int i=1; i<num.length;i++){
if(count==0){
count++;
major=num[i];
}else if(major==num[i]){
count++;
}else count--;
}
return major;
}
这是讨论中对该解法的一个说明:
1)According to problem Major element appears more than ⌊ n/2 ⌋ times
2)Count is taken as 1 because 1 is the least possible count for any number ,
3)Major element is updated only when count is 0 which means --Array has got as many non major elements as major element
- Check this case 1,1,3,3,5,3,3 ---Majority element is 1 initially count becomes 0 at after 2nd 3 and 5 is made as majority element with count=1 and finally 3 is made as major element.
Major Element是出现次数大于n/2的一个数,遍历这个数组时,只有Major Element才能保证count>0.
169. Majority Element - LeetCode的更多相关文章
- 169 Majority Element [LeetCode Java实现]
题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 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 ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【一天一道LeetCode】#169. Majority Element
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- html5中常被忘记的标签,属性
placeholder placeholder是input中的属性,就是默认输入的text,当用户输入时,text会被清空. 用法 <input type ="text" p ...
- 云集,让 web app 像 native app 那样运行(雄起吧,Web 开发者)
让 web app 像 native app 那样运行 云集是一个轻应用(即 web app)的运行环境,可以让 web app 像 native app 那样运行. just like this 这 ...
- A小程序与B小程序相互跳转的一点记录
要点速览: A小程序和B小程序关联同一个公众号 B程序的用户授权 A小程序和B小程序的用户关联 诸葛 io 统计用户访问信息 需求:微信放开小程序互跳的 API 后,一些导流和拉新等活动可以在新的小程 ...
- python-产生每位数字相同的n位数
读入2个正整数A和B,1<=A<=9, 1<=B<=10,产生数字AA...A,一共B个A 输入格式: 在一行中输入A和B. 输出格式: 在一行中输出整数AA...A,一共B个 ...
- Spark入门之环境搭建
本教程是虚拟机搭建Spark环境和用idea编写脚本 一.前提准备 需要已经有搭建好的虚拟机环境,具体见教程大数据学习之路又之从小白到用sqoop导出数据 - 我试试这个昵称好使不 - 博客园 (cn ...
- 子线程中如何修改ui界面
1.Android进程 一个应用程序被启动时,系统默认创建执行一个叫做"main"的线程.这个线程也是你的应用与界面工具包(android.widget和android.view包 ...
- EMS修改邮箱容量限制的方法
使用PowerShell命令完成邮箱数据库限制任务. 以Exchange管理员身份打开EMS控制台.在PowerShell命令提示符下,键入如下命令. Set-MailboxDatabase Test ...
- C++---初识C++
C和C++的关系 C语言是结构化和模块化的语言, 面向过程. C++是在C语言的基础上, 增加了面向对象的机制, 并对C语言的功能进行了扩充. 变量的定义可以出现在程序中的任何行 提供了标准输入输出流 ...
- 北桥芯片(north bridge/host bridge)
看下上面的图,会比较清晰的认识到北桥芯片所在位置 北桥芯片(North Bridge) 是mother board chipset(主板芯片组) 中起主导作用的最重要的组成部分,也称为主桥(Host ...
- Hyperledger Fabric节点的动态添加和删除
前言 在Hyperledger Fabric组织的动态添加和删除中,我们已经完成了在运行着的网络中动态添加和删除组织.本文将在其基础上,详细介绍了如何在 soft 组织上添加新的 peer2 节点,并 ...