[LeetCode] 169. Majority Element 多数元素
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.
Example 1:
Input: [3,2,3] Output: 3
Example 2:
Input: [2,2,1,1,1,2,2] Output: 2
给一个大小为n的数组,找出它的多数元素。数组非空,多数元素存在。多数元素:出现次数超过n/2的元素(超过数组长度一半)。
解法1: 用两个循环跟踪统计最多次数的元素。 T:O(n*n) S: O(1)
解法2: BST, T:O(nlogn) S: O(n)
解法3:Boyer-Moore多数投票算法 Boyer–Moore majority vote algorithm,T:O(n) S: O(1)
解法4: HashMap, T:O(n) S: O(n)
Java:
public class Solution {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;}
}
Java:
public class Solution {public int majorityElement(int[] nums) {int res = 0, cnt = 0;for (int num : nums) {if (cnt == 0) {res = num; ++cnt;}else if (num == res) ++cnt;else --cnt;}return res;}
}
Python: T: O(n), S: O(1)
class Solution:def majorityElement(self, nums):idx, cnt = 0, 1for i in xrange(1, len(nums)):if nums[idx] == nums[i]:cnt += 1else:cnt -= 1if cnt == 0:idx = icnt = 1return nums[idx]
C++:
class Solution {
public:int majorityElement(vector<int>& nums) {int ans = nums[0], cnt = 1;for (const auto& i : nums) {if (i == ans) {++cnt;} else {--cnt;if (cnt == 0) {ans = i;cnt = 1;}}}return ans; }
};
类似题目:
[LeetCode] 229. Majority Element II 多数元素 II
All LeetCode Questions List 题目汇总
转载于:https://www.cnblogs.com/lightwindy/p/8606860.html
[LeetCode] 169. Majority Element 多数元素相关推荐
- [勇者闯LeetCode] 169. Majority Element
[勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...
- 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(Majority Voting Algorithm)
Majority Element Given an array of size n, find the majority element. The majority element is the el ...
- 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 (多数投票问题)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode]: 169: Majority Element
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- [swift] 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
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- [Easy] 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
最新文章
- 非计算机专业想学习Python,有哪些问题需要注意?
- 树莓派应用实例6:测量土壤湿度(改进WEB发布)
- Codeforces 427 D. Match amp; Catch
- akka与neety_Akka STM –与STM Ref和Agent一起打乒乓球
- python 学习资源收集汇总
- 三种钱非常奥妙 花越多就赚越多
- python启动http服务_Python命令开启http.server服务器
- 苹果Mac全能视频播放器:Playr
- thinkpadx1mdt 网络启动_二二、MDT 2013 Update 1批量部署-硬件驱动使用、驱动库建立及自动识别...
- ckfinder php 漏洞,编辑器漏洞
- mil mm 单位换算
- Vscode工作区调试(虚拟环境)配置指北
- python一键去PDF水印,只需十行代码,超级简单...
- CheckBoxList复选框例子
- android 汉字 unicode编码,Android解析UniCode编码
- npm配置国内镜像(淘宝镜像)
- IP、域名和端口号之间的联系
- 如何进行一次大批量部署服务器安装?
- 软件测试十八阶段(linux操作系统)
- 几米的漫画<地下佚>的摘录