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.

求主元素:包括n个元素的数组中。假设一个元素的出现次数大于n/2(向下取整)次。则该元素为主元素。
依据定义,则每两个数中必有一个是主元素,设定一个变量cnt=0。当两个数同样时。cnt++,不同一时候,cnt--。用变量
majority来记录主元素,每当cnt=0时,把当前元素赋给majority。成为候选的主元素。这样一来。最后的majority的值一定就是主元素。
代码例如以下:
class Solution {
public:
int majorityElement(vector<int> &num) {int majority;int cnt = 0;for(int i=0; i<num.size(); i++){if ( cnt ==0 ){majority = num[i];cnt++;}else{majority == num[i] ? cnt++ : cnt --;if (cnt > num.size()/2) return majority;}}return majority;}
};

Majority Element:主元素相关推荐

  1. [LeetCode] 169. Majority Element 多数元素

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

  2. 主元素问题 Majority Element

    2018-09-23 13:25:40 主元素问题是一个非常经典的问题,一般来说,主元素问题指的是数组中元素个数大于一半的数字,显然这个问题可以通过遍历计数解决,时间复杂度为O(n),空间复杂度为O( ...

  3. Count Min Sketch: from Finding the Majority Element problem to heavy hitter problem,统计元素频率的利器

    这是笔者学习 Stanford cs 168 课程的一些学习笔记 lecture 2, 主要讲一个基于 hash 和独立试验思想,设计的一种数据结构 count min sketch,想法非常类似于 ...

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

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

  6. [Array]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

    leetcode-169. Majority Element Given an array of size n, find the majority element. The majority ele ...

  8. 【LeetCode 169】Majority Element

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

  9. 169. Majority Element

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

最新文章

  1. 华为怎么改输入法皮肤_搜狗输入法皮肤挑花眼?这次皮肤效果也能划着看了
  2. (C++)对用户输入的整形数组进行冒泡排序
  3. ASP.NET-FineUI开发实践-14
  4. java的collection集合
  5. hdu 2191 多重背包
  6. 【Leetcode】岛屿问题(数量,周长,面积)
  7. python提供什么机制处理程序运行错误_浅谈Python异常处理机制
  8. 中3d库后接负载_Nature子刊:近红外光交联水凝胶用于活体生物3D打印
  9. 漫步最优化四十五——矩阵S的生成
  10. 常见RPM,YUM,DNF指令
  11. 全球开发者调查:JavaScript 成最爱,还最喜欢用 React
  12. 中电信将推出软件商店 或利用微软Google平台
  13. 31省份RD经费内部支出、全时当量、专利数、技术市场成交额(1997-2019年)
  14. 魔百盒cm201-1YS不识别
  15. 联通光纤猫虚拟服务器设置,【2017年整理】联通光猫配置操作手册.doc
  16. 【DP】LeetCode91 解码方法 【java】
  17. 【AI实战】手把手教你深度学习文字识别(文字检测篇:基于MSER, CTPN, SegLink, EAST等方法)...
  18. 网易相册找回服务器,再见了,网易相册!24日起关闭服务器,不保留数据
  19. 实验四:201571030116/201571030106《小学四则运算练习软件需求说明》结对项目报告...
  20. NAND Flash和 NOR Flash的区别

热门文章

  1. 王恩哥院士:信息化发展进程中,科学、基础研究和技术、应用都是关键
  2. Facebook爆锤深度度量学习:该领域13年来并无进展!网友:沧海横流,方显英雄本色...
  3. 借力5G,云VR将推动VR产业迎“第二春”
  4. 关于大脑未必是破解智能和意识之谜的钥匙探讨
  5. 《麻省理工科技评论》:2018年18大科技趋势,2017年7大失败技术
  6. 神经网络并不是尚方宝剑,我们需要正视深度 NLP 模型的泛化问题
  7. Gartner:预计2018年人工智能行业总价值达1.2万亿美元
  8. 神经尘埃、脑波打字…… 2018年的脑科学要研究什么?
  9. 女程序员也有 35 岁危机焦虑吗?
  10. IT 往事录:苹果 Mac 之父,却在 Mac 问世前黯然退场