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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

两种解法,一种是排序后取中位数!

另外一种是用计数的方式,Moore voting algorithm 据说是叫这个,和选票一半以上的人当选一样,计票方法:如果是一张反对票一张赞成票则互相抵消。

class Solution(object):def majorityElement(self, nums):""":type nums: List[int]:rtype: int"""ans = nums[0]cnt = 1        for i in xrange(1, len(nums)):if cnt == 0:cnt = 1ans = nums[i]elif nums[i] == ans:cnt += 1else:cnt -= 1        return ans

另外我自己的解法是每两位计数一次,计数重复出现的数,见注释示例:

class Solution(object):def majorityElement(self, nums):""":type nums: List[int]:rtype: int"""#nums.sort()#return nums[len(nums)>>1]# 1 1 2=>1==1 ans=1, dup=2, last 2 dump it# 2 1 1=>2!=1, dup=0, ans=1# 1 1 1 2=>1=1, dup=2, ans=1, 1!=2 # 1 2 3 1 1 1=>1!=2, dup=0, 3!=1, dup=0, 1==1, dup=2, ans=1# 1 1 3 3 2 1 1=>1==1,dup=2, 3==3, dup-=2,dup=0, 2!=0, 1 is answer# 1 1 1 1 3 3 3 3 2 1 1=>ans = nums[0]dup_flag = 0i = 1while i<len(nums):if nums[i] == nums[i-1]: if nums[i] == ans: # check if is prev ansdup_flag += 2                else:if dup_flag >= 2:dup_flag -= 2else:dup_flag = 2ans = nums[i]                                           i += 2if len(nums) & 1 and dup_flag == 0:return nums[-1]return ans

最后一种是位运算:

Bit Manipulation

Another nice idea! The key lies in how to count the number of 1's on a specific bit. Specifically, you need a mask with a 1 on the i-the bit and 0 otherwise to get the i-th bit of each element in nums. The code is as follows.

class Solution {
public:int majorityElement(vector<int>& nums) { int major = 0, n = nums.size(); for (int i = 0, mask = 1; i < 32; i++, mask <<= 1) { int bitCounts = 0; for (int j = 0; j < n; j++) { if (nums[j] & mask) bitCounts++; if (bitCounts > n / 2) { major |= mask; break; } } } return major; } };

转载于:https://www.cnblogs.com/bonelee/p/8654829.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 (多数投票问题)

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

  4. [LeetCode]: 169: Majority Element

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

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

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

  8. leetcode讲解--169. Majority Element

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

  9. LeetCode 229 : Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

最新文章

  1. 在UltraEdit中使用正则表达式
  2. WPF中splashScreen启动程序之前出现一个过程动画的效果
  3. ssl1063-统计数字【哈希表】
  4. c语言猜拳游戏实验报告,《猜拳小游戏》--一个c语言写的小项目
  5. Maven新建项目的JDK版本类型问题
  6. oracle中的new old 关键字
  7. 请求过程中,需要证书认证,这种情况下如何处理
  8. IDEA如何开启远程调试
  9. cad字体安装_拿了CAD字体不知道怎么装?看过来
  10. 基于javaweb+JSP+Servlet火车票网上订票系统(前台、后台)
  11. 光耦驱动单向可控硅_超低功耗光电耦合驱动单向可控硅电路,光电耦合器
  12. SIM868烧写自己MT2503开发的程序过程
  13. 大容量sd卡reread之后/dev下概率性出现无设备文件
  14. 百度云盘MP3音乐外链、视频外链教程
  15. 嵌入式操作系统介绍之 NuttX
  16. php转java学什么书,推荐给java软件工程师以及从java转向php程序猿的一本好书
  17. php+mysql(5.1)+apache+centos 编译安装
  18. 数字电路之Verilog红绿灯设计
  19. px 、 rem 、 vw学习
  20. idea每次打开总是一直加载indexing library‘maven xxx‘‘,Scanning file to index,如何解决?

热门文章

  1. adb devices检测不到夜神模拟器的解决办法
  2. thymeleaf rs 查询结果_第十一章 JDBC与MySQL数据库(10)——通用查询
  3. python表单数据系统_使用MultipartPostHandler用Python发布表单数据
  4. mysql字符集查看_查看和设置mysql字符集
  5. 手机号验证_国际手机号收不到微博验证短信,微博验证短信一直提示超过上限怎么办?...
  6. 通俗易懂的讲解区块链
  7. 字节跳动内部学习资料泄露!mysql的安装与配置
  8. python【数据结构与算法】最短路算法之FloyedDijkstra
  9. 机器学习(MACHINE LEARNING)MATLAB动态规划解决背包问题
  10. 桶排序python实现