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 多数元素的更多相关文章

  1. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  2. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  3. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  4. leetcode——169 Majority Element(数组中出现次数过半的元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. Java for LeetCode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

随机推荐

  1. Mac下安装oh my zsh之后配置环境变量失效问题

    背景:在刚拿到mac 的时候,使用了默认的bash,由于工作需要在电脑上安装了maven,在~/.bash_profile 文件中添加了maven的配置如下 $ cat ~/.bash_profile ...

  2. Mysql【第三课】

  3. Winform异常处理之ThreadException、unhandledException及多线程异常处理

    异常处理之ThreadException.unhandledException及多线程异常处理 一:ThreadException和unhandledException的区别 处理未捕获的异常是每个应 ...

  4. win10在ip变动之后重启ftp

    自己的笔记本拿去公司工作一周后,回到家里发现自己的ftp服务器没办法访问了. 首先,确定自己的ftp服务是打开的 接着搜索internet information service打开 接着点击网站查看 ...

  5. volatile 错误示范做线程同步 demo

    这里 http://hedengcheng.com/?p=725 有对volatile 非常详细的解释,看完之后,心里一惊,因为我刚好在一个项目里用了文中错误示范那种方式来做线程同步,场景如下: Th ...

  6. eclipse 配置python环境 json 插件

    windows->install new software add 配置python 环境: name:pydev(可随意写) url:http://pydev.org/updates/ (如果 ...

  7. Oracle 分区默认segment大小变化(64k—>8M)

    原文链接:http://www.cnblogs.com/wcwen1990/p/6656545.html _partition_large_extents和_index_partition_large ...

  8. 看图轻松理解数据结构与算法系列(NoSQL存储-LSM树) - 全文

    <看图轻松理解数据结构和算法>,主要使用图片来描述常见的数据结构和算法,轻松阅读并理解掌握.本系列包括各种堆.各种队列.各种列表.各种树.各种图.各种排序等等几十篇的样子. 关于LSM树 ...

  9. Windows本机调试内部组件

    将详细分析Windows调试的本机接口.希望读者对C和通用NT内核体系结构和语义有一些基本的了解.此外,这并不是介绍什么是调试或如何编写调试器.它可以作为经验丰富的调试器编写人员或好奇的安全专家的参考 ...

  10. PCA与ICA

    关于机器学习理论方面的研究,最好阅读英文原版的学术论文.PCA主要作用是数据降维,而ICA主要作用是盲信号分离.在讲述理论依据之前,先思考以下几个问题:真实的数据训练总是存在以下几个问题: ①特征冗余 ...