[Leedcode 169]Majority Element
1 题目描述
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.
2 分析
求主元素有两种O(n)量级的算法,一种是堆栈法,若栈为空或当前元素与栈顶元素相同,进栈,否则出栈。最后栈顶元素变为主元素。
另一种做法是芯片法,使用分治策略,比堆栈法要麻烦,主要是每次比较两个数字,不同,两个都扔掉,相同,留下一个,最后剩下的肯定是主元素。
3 代码
堆栈代码。
public int majorityElement(int[] num)
{
int majorrityElement = 0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < num.length; i++) {
if (stack.isEmpty()) {
stack.push(num[i]);
}else{
if (stack.peek() == num[i]) {
stack.push(num[i]);
}else {
stack.pop();
}
}
}
majorrityElement = stack.peek();
return majorrityElement;
}
芯片法有点麻烦,也没写。
[Leedcode 169]Majority Element的更多相关文章
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 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 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 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 - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- (Array)169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- \\Device\\PhysicalMemory
从Windows Server 2003 with SP1 以后就禁用了用户态访问\\Device\\PhysicalMemory,要访读取SMBIOS的信息,请使用以下API:•EnumSystem ...
- &和&&的区别是什么
1)&是按位与操作符,a&b是把a和b都转换成二进制数后,然后再进行按位与的运算.&&是逻辑与运算符,a&&b就是当且仅当两个操作数都为true时,其结 ...
- 跟我学Spring Boot(三)Spring Boot 的web开发
1.Web开发中至关重要的一部分,Web开发的核心内容主要包括内嵌Servlet容器和SpringMVC spring boot 提供了spring-boot-starter-web 为web开发提 ...
- 【UI测试】--美观与协调性
- Hibernate学习笔记:注解@OneToMany和@ManyToOne的单独使用问题 不成对使用
以某个实际场景为例,现在两张表:用户表User 订单表Order:很显然用户对订单是一对多的关系.二者注解如下 用户表User @Entity @Table(name="users" ...
- 2016年3月31日_应化所群体Review
Review目的: Phonegap的ble插件可以接收到设备发送的信息,但接收并在控制台显示的数据夏一鸣不知道是正确的数据,还是由于编码解码问题导致的乱码问题.此次Review要解决的问题即判断接收 ...
- Windows 8.1 新控件和功能:
http://msdn.microsoft.com/zh-cn/library/windows/apps/bg182878.aspx#five 将 XAML 树呈现为位图: 适用于 Windows 8 ...
- 全球IT经理一致喜爱的ITSM系统、it服务台软件
- IDEA导入MySQL包
点击[Project Structure] 点击[Modules] 在点击下面的界面 找到自己下载的MySQL包就OK了
- KAFKA 监控管理界面 KAFKA EAGLE 安装
概述 Kafka Eagle监控系统是一款用来监控Kafka集群的工具,目前更新的版本是v1.2.3,支持管理多个Kafka集群.管理Kafka主题(包含查看.删除.创建等).消费者组合消费者实例监控 ...