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.

思路:

  找到一个数组中出现次数超过一半的数。排序、哈希等方法就不提了,考虑O(n)的复杂度,有一种传说中的摩尔投票算法可用。其实也很简单,初始选第一个元素,依次比对它和其它的元素,若相等则其票数加一,反之则减一,当其票数为0时,更换投票对象为当前的元素,继续执行,最终即可得结果(前提的数组中存在这样的数,否则结果不确定是什么,依赖于元素的摆放顺序)。

C++:

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

Python:

 class Solution:
# @param {integer[]} nums
# @return {integer}
def majorityElement(self, nums):
ret, cnt = 0, 0 for val in nums:
if cnt == 0:
ret = val
cnt = 1
else:
if ret == val:
cnt = cnt + 1
else:
cnt = cnt - 1
return ret

【LeetCode 169】Majority Element的更多相关文章

  1. 【LeetCode 229】Majority Element II

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

  2. 【LeetCode OJ】Majority Element

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

  3. Leetcode # 169, 229 Majority Element I and II

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

  4. 【LeetCode OJ】Remove Element

    题目:Given an array and a value, remove all instances of that value in place and return the new length ...

  5. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  6. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  7. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  8. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  9. 【LeetCode题解】350_两个数组的交集Ⅱ

    目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...

随机推荐

  1. MQTT客户端与服务代理的案列

    服务端,采用 Mosquitto 来转发分发消息. 客户端自己写. 服务端 启动 mosquitto (底下的命令是我自己放到环境变量里面的,通过alias 运行mosquitto) Ishallbe ...

  2. 欧拉工程第58题:Spiral primes

    题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; impo ...

  3. python学习[一]

    Vamei写了很好的python教程,感谢:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html 摘录笔记 print命令行模式: ...

  4. JavaScript之this,new,delete,call,apply(转)

    JavaScript之this,new,delete,call,apply 1.this 一般而言,在Javascript中,this指向函数执行时的当前对象. 2.new 在JavaScript中, ...

  5. HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)

    Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...

  6. 分批次获取git for windows的源代码

    $ git initInitialized empty Git repository in d:/SourceCode/GitHub/Git For Windows/Git/.git/ $ git r ...

  7. th固定 td滚动的表格实现

    为什么这样? 体验好 原理 通过两个表格,使其th td 对应,产生一种错觉. 代码 1.html <div class="content"> <div clas ...

  8. uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)

    题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...

  9. 函数mem_pool_fill_free_list

    /********************************************************************//** Fills the specified free l ...

  10. BZOJ2253: [2010 Beijing wc]纸箱堆叠

    题解: 其实就是求三维偏序最长链.类似于三维逆序对,我们可以用树状数组套平衡树来实现. DP方程 :f[i]=max(f[j]+1) a[j]<a[i] 我们按一维排序,另一位建立树状数组,把第 ...