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. CentOS查看系统版本号

    命令:cat /etc/redhat-release [elsearch@localhost data]$ cat /etc/redhat-release Red Hat Enterprise Lin ...

  2. 计数排序Counting sort

    注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 ...

  3. java 8新特性 instant

    Java 8目前已经开始进入大众的视线,其中笔者在写本文之前,留意到其中Java 8预览版中将会出现新的关于日期和时间的API(遵守JSR310规范).在本系列文章中,将对这些新的API进行举例说明. ...

  4. android抽屉导航的设计准则

    我阅读了google官方的关于抽屉导航的设计准则,这可以给我带来什么帮助?最起码,我可以知道,抽屉导航适用在什么场景中,使用它时要注意什么事项.App的设计是有规则可以依据的,比如,使用抽屉导航时,是 ...

  5. 「6月雅礼集训 2017 Day11」delight

    [题目大意] 有$n$天,每天能吃饭.睡觉.什么事也不干 每天吃饭的愉悦值为$e_i$,睡觉的愉悦值为$s_i$,什么都不干愉悦值为0. 要求每连续$k$天都要有至少$E$天吃饭,$S$天睡觉. 求最 ...

  6. 【CodeForces】601 D. Acyclic Organic Compounds

    [题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...

  7. hdu 1200 To and Fro(简单模拟或DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1200 To and Fro Time Limit: 2000/1000 MS (Java/Others ...

  8. hdu 1233 还是畅通工程 (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    ...

  9. ShellCode的几种调用方法

    ShellCode是一种漏洞代码,中文名也叫填充数据,一般是用C语言或者汇编编写.在研究的过程中,自己也学到了一些东西,发现其中也有许多坑,所以贴出来,如果大家有碰到的,可以参考一下. 以启动电脑上的 ...

  10. vmware安装ubuntu " Intel VT-x 处于禁用状态"

    vmware安装ubuntu " Intel VT-x 处于禁用状态" http://jingyan.baidu.com/article/fc07f98976710e12ffe51 ...