[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, 1 for i in xrange(1, len(nums)):
if nums[idx] == nums[i]:
cnt += 1
else:
cnt -= 1
if cnt == 0:
idx = i
cnt = 1 return 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 题目汇总
[LeetCode] 169. Majority Element 多数元素的更多相关文章
- 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] ...
- 23. leetcode 169. Majority Element
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 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for 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 a ...
随机推荐
- docker学习2-快速搭建centos7-python3.6环境
前言 当我们在一台电脑上搭建了python3.6的环境,下次换了个电脑,或者换成linux的系统了,又得重新搭建一次,设置环境变量.下载pip等操作. 好不容易安装好,一会Scrips目录找不到pip ...
- requireJS的基本使用
requireJS的基本使用 一.总结 一句话总结: requireJS是js端模块化开发,主要是实现js的异步加载,和管理模块之间的依赖关系,便于代码的编写和维护 1.页面加载的js文件过多的缺点是 ...
- 深度学习Keras框架笔记之Dense类(标准的一维全连接层)
深度学习Keras框架笔记之Dense类(标准的一维全连接层) 例: keras.layers.core.Dense(output_dim,init='glorot_uniform', activat ...
- 大数据开发keras框架环境配置小结
系统安装问题 win10+ubuntu16.04 在win10在需要security boot设置成disable,否则安装完后无法设置启动项. 安装完ubuntu重启,系统会直接进入win10,需要 ...
- 36氪新风向 | 三个月估值普涨三倍,你未来的RPA机器人同事正在路上
http://www.sohu.com/a/320208242_114778 2019 年 4 月,关注 RPA 赛道的投资人 Kevin(化名)就发现,仅仅过了一个春节,自己所在的早期基金已经投不起 ...
- Joint Approximative Diagonalization of Eigen matrix (JADE)
特征矩阵联合相似对角化算法[1]. Cardoso于1993年提出的盲信号分离具有代表性的一种算法.是一种基于四阶累积量特征矩阵近似联合对角化盲分离算法.该算法将目标函数最大化问题等价于一组四阶累积量 ...
- 使用go初步调用etcd
使用go初步調用etcd package main import ( "context" "go.etcd.io/etcd/clientv3" "ti ...
- window对象方法(open和close)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- idea中properties配置文件 注释显示中文乱码问题
- Lightning Web Components 组合(五)
使用组合我们可以用来设计复杂的组件. 组合一些比较小的组件,可以增加组件的重新性以及可维护性. 通过以下一个简单的demo,将会展示关于owner 以及container 的概念,在实际的项目中 ex ...