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 多数元素相关推荐

  1. [勇者闯LeetCode] 169. Majority Element

    [勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...

  2. Leetcode 169 Majority Element

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

  3. 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 ...

  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

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  7. [swift] 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

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

  9. [Easy] 169. Majority Element

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

最新文章

  1. 非计算机专业想学习Python,有哪些问题需要注意?
  2. 树莓派应用实例6:测量土壤湿度(改进WEB发布)
  3. Codeforces 427 D. Match amp; Catch
  4. akka与neety_Akka STM –与STM Ref和Agent一起打乒乓球
  5. python 学习资源收集汇总
  6. 三种钱非常奥妙 花越多就赚越多
  7. python启动http服务_Python命令开启http.server服务器
  8. 苹果Mac全能视频播放器:Playr
  9. thinkpadx1mdt 网络启动_二二、MDT 2013 Update 1批量部署-硬件驱动使用、驱动库建立及自动识别...
  10. ckfinder php 漏洞,编辑器漏洞
  11. mil mm 单位换算
  12. Vscode工作区调试(虚拟环境)配置指北
  13. python一键去PDF水印,只需十行代码,超级简单...
  14. CheckBoxList复选框例子
  15. android 汉字 unicode编码,Android解析UniCode编码
  16. npm配置国内镜像(淘宝镜像)
  17. IP、域名和端口号之间的联系
  18. 如何进行一次大批量部署服务器安装?
  19. 软件测试十八阶段(linux操作系统)
  20. 几米的漫画<地下佚>的摘录

热门文章

  1. Spring学习(二)Spring IoC 和 DI 简介
  2. 入手腾龙SP AF90mm MACRO
  3. 文本处理三剑客之 awk
  4. 微信小程序---setData
  5. Python内置函数之--open
  6. openSUSE中启用apache mod_rewrite
  7. wordpress后台404页面
  8. VS2012 生成项目报 Lc.exe已退出,代码为-1 错误
  9. css圆角box(网上流行用b标签)
  10. 07-01-安装-Exchange Server 2019 on Win 2019 Core