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.

需要注意的是:这道题只是要找出多数元素,已经默认存在多数元素了,而不需要去判断是否存在多数元素。之前的思路就一直卡在怎么判断多数元素存的的问题上了。

思路解析:

1. 初始化majorityIndex,并且维护其对应count;

2. 遍历数组,如果下一个元素和当前候选元素相同,count加1,否则count减1;

3. 如果count为0时,则更改候选元素,并且重置count为1;

4. 返回A[majorityIndex]

原理:如果majority元素存在(majority元素个数大于n/2,个数超过数组长度一半),那么无论它的各个元素位置是如何分布的,其count经过抵消和增加后,最后一定是大于等于1的。 如果不能保证majority存在,需要检验。 复杂度:O(N)

Attention: 循环时从i = 1开始,从下一个元素开始,因为count已经置1

C++版:

class Solution {
public:
int majorityElement(vector<int> &num) { int elem = ;
int count = ; for(int i = ; i < num.size(); i++) { if(count == ) {
elem = num[i];
count = ;
}
else {
if(elem == num[i])
count++;
else
count--;
} }
return elem;
}
  };

Python版:

class Solution:
# @param {integer[]} nums
# @return {integer}
def majorityElement(self, nums):
lenth=len(nums)
index=0
count=1
for i in range(lenth):
if nums[index]==nums[i]:
count+=1
else:
count-=1
if count==0:
index=i
count+=1
return nums[index]

Majority Element——算法课上的一道题(经典)的更多相关文章

  1. Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)

    Reverse Linked List I Question Solution Reverse a singly linked list. Reverse Linked List I 设置三个指针即可 ...

  2. Median of Two Sorted Arrays——算法课上经典的二分和分治算法

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. 【算法31】寻找数组的主元素(Majority Element)

    题外话 最近有些网友来信问我博客怎么不更新了,是不是不刷题了,真是惭愧啊,题还是在刷的,不过刷题的频率没以前高了,看完<算法导论>后感觉网上很多讨论的题目其实在导论中都已经有非常好的算法以 ...

  4. leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

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

  5. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...

  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. leetcode169——Majority Element (C++)

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

  8. LeetCode169:Majority Element(Hash表\位操作未懂)

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

  9. leetcode 【 Majority Element 】python 实现

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

随机推荐

  1. Trades FZU - 2281 (大数+贪心)

    This is a very easy problem. ACMeow loves GTX1920. Now he has m RMB, but no GTX1920s. In the next n ...

  2. mysql的时间函数整理

      转:这里总结的非常齐全: http://fengbin2005.iteye.com/blog/1999763   Mysql时间函数 对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描 ...

  3. select和epoll概念

    关于linux的I/O复用接口select和epoll,下列说法错误的是() select调用时会进行线性遍历,epoll采用回调函数机制,不需要线性遍历 select的最大连接数为FD_SETSIZ ...

  4. UBOOT启动内核过程

    1.摘要 (1).启动4步骤第一步:将内核搬移到DDR中第二步:校验内核格式.CRC等第三步:准备传参第四步:跳转执行内核(2).涉及到的主要函数是:do_bootm和do_bootm_linux(3 ...

  5. JavaScript知识递归实现数组中指定后代元素的查找

    描述:要求将下面的数据类型中每个子孙后代id放入一个数组并返回 var arr = [ {"id":28,"text":"公司信息", &q ...

  6. JavaScript中this的用法详解

    JavaScript中this的用法详解 最近,跟身边学前端的朋友了解,有很多人对函数中的this的用法和指向问题比较模糊,这里写一篇博客跟大家一起探讨一下this的用法和指向性问题. 1定义 thi ...

  7. mysql 中 group_concat()用法

     基本语法:group_concat([DISTINCT] 要连接的字段 [Order BY  排序字段 ASC/DESC] [Separator '分隔符']) 初始数据:              ...

  8. MySQL查看所有用户及拥有权限

    查看MYSQL数据库中所有用户 mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM m ...

  9. tortoise svn冲突解决

    Tortoiese svn 冲突解决 当文件被别人修改并提交到SVN服务器后,如果自己本地的文件没有被更新为最新的版本,而且已经做了修改,这时候提交将会被成功,系统会提示你的版本已经过期,并要求你先进 ...

  10. 2015/9/1 Python基础(6):列表

    列表和字符串类型很相似,是同样的序列式数据类型.但是字符串只能由字符组成,列表可以保留任意数目的Python对象的灵活的容器.Python的列表比C的数组要灵活,数组里面只能是一种类型,列表可以有多种 ...