169. Majority Element

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.

找出数列中个数超过一半的数,假定数列不为空且该数一定存在。

代码如下:

 class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
int index = ;
int candidate = nums[];
for(int i = ; i < n; i++)
{
if(candidate == nums[i])
{
index ++;
}
else
{
index --;
}
if(index < )
{
candidate = nums[i];
index = ;
}
}
return candidate;
}
};

leetcode 169的更多相关文章

  1. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  2. 23. leetcode 169. Majority Element

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

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

  5. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  6. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

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

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

  8. Java实现 LeetCode 169 多数元素

    169. 多数元素 给定一个大小为 n 的数组,找到其中的多数元素.多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输 ...

  9. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

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

随机推荐

  1. Java与数据库之间时间的处理

    Java与数据库之间时间的处理 在数据库中建表: DROP TABLE IF EXISTS `times`; CREATE TABLE `times` ( `id` int(11) NOT NULL ...

  2. Java甘特图控件swing版免费下载地址

    FlexGantt 控件是现在Java 平台下最先进的甘特图解决方案,使用一个很高的抽象层次,能适用于多种不同的域,例如 ERP 系统.生产计划和日程安排.制造流程系统或项目公文管理程序等.这些使得 ...

  3. C++类的嵌套(2)-访问权限和调用关系

    类似于命名空间,一个类也是一个类命名空间.因此类嵌套的作用是帮助实现外层类,并且避免命名冲突.  对于命名空间(不再赘述可以参考<c++ prime plus>),其中定义的变量和函数的作 ...

  4. iOS- UITextField限制输入长度

    限制输入长度的问题,在这里完美的解决了! //先创建一个textField 和 一个button. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  5. LintCode Search For a Range (Binary Search)

    Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较. 循环终止条件: 最后剩两数比较(while(left + 1 < righ ...

  6. 【转】Jquery ajax方法解析返回的json数据

    转自http://blog.csdn.net/haiqiao_2010/article/details/12653555 最近在用jQuery的ajax方法传递接收json数据时发现一个问题,那就是返 ...

  7. Squid configuration directives 3.0

    WELCOME TO SQUID 3.0.STABLE25-20100412 ---------------------------- This is the default Squid config ...

  8. 2016HUAS_ACM暑假集训3C - Til the Cows Come Home

    单源最短路径,首先想到的是Dijkstra.Dijkstra算法的思路就不啰嗦了,概括起来就是时刻保持当前节点到目标节点的距离最短. 题目大意(不进行翻译解释了,就抽离为图来表达):有N个顶点和T条边 ...

  9. msp430FR5739 FRAM的学习

    FRAM,中文名称为铁电存储器..FRAM提供一种与RAM一致的性能,但又有与ROM 一样的非易失性. FRAM 克服以上二种记忆体的缺陷并合并它们的优点,它是全新创造的产品,一个非易失性随机存取储存 ...

  10. 【Unity3D基础教程】给初学者看的Unity教程(三):通过制作Flappy Bird了解Native 2D中的Sprite,Animation

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 上一次我们讲了MonoBehaviou ...