[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 ...
随机推荐
- Java 使用jdk自带的wsimport命令生成webservice客户端代码
wsimport -s E:\workspace\givemewords\src -p com.test.service -keep http://localhost:8085/Service/Fun ...
- 设计师都爱用的UI标注软件有哪些?
UI标注软件现在是设计师(UI.PM.前端等)必备的一款软件.设计稿是UI设计师日常工作中的产出物之一,当然,做出了高保真设计稿并不意味着你的工作结束了,因为你还得与下游的开发工程师进行对接. 我们经 ...
- maven 介绍(zz )
Maven 编辑 目录 1简介 2特点 3常用命令 4推荐书籍 5Win7配置 6生命周期 1 1简介 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构 ...
- 用EventLog Analyzer的预定义报表和告警来进行Syslog管理
用EventLog Analyzer的预定义报表和告警来进行Syslog管理 系统日志(Syslog)管理是几乎所有企业的重要需求.系统管理员将syslog看作是解决网络上系统日志支持的系统和设备性能 ...
- chmod / chown /chattr
显示了七列信息,从左至右依次为:权限.文件数.归属用户.归属群组.文件大小.创建日期.文件名称 d :第一位表示文件类型 d 文件夹 - 普通文件 l 链接 b 块设备文件 p 管道文件 c 字符设备 ...
- 819. Most Common Word
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 2018.10.24 NOIP模拟 小 C 的宿舍(分治)
传送门 分治妙题. 没有这道题的暴力分今天又垫底了啊233 由于用了分治的方法,我们只用考虑左区间对右区间的贡献以及右区间对左区间的贡献. 可以发现如果从中点开始向两边递推最小值并用这个区间最小值来推 ...
- TCP/IP协议(5):传输层之TCP
一.TCP报文 上图为TCP报文的格式,可以看到TCP头部占20个字节,其中红色圆圈中每一项占一位,表示TCP报文的类型,置1表示该项有效. SYN表示建立连接. FIN表示关闭连接. A ...
- 普通用户修改.bash_profile 权限问题
例如oracle用户想要修改它下面的.bash_profile文件: 在命令行运行: [root@localhost ~]# ls -lh /home/oracle/.bash_profile
- vba遗传算法之非一致性突变
http://www.docin.com/p-959323141-f4.html Sub 非一致性变异() Dim totalGenerate As Integer, currentGenerate ...