169. Majority Element
2016-04-25 09:07
309 查看
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.
You may assume that the array is non-empty and the majority element always exist in the array.
public class Solution { public int majorityElement(int[] nums) { int n = nums.length; int candidate = 0; int times = 0; for(int i = 0; i < n; i++){ if(times == 0) candidate = nums[i]; if(nums[i] != candidate){ times--; } else{ times++; } } return candidate; } }
相关文章推荐
- 设计模式读书笔记之中介者模式(mediator pattern)
- 设计模式读书笔记之享元模式(Flyweight pattern)
- LintCode: Fizz Buzz
- 如何设计好词袋模型BoW模型的类类型
- win7 64位4GB内存下 tomcat7扩大内存
- 第三届百草味517吃货节即将盛大开启,跨界合作成为最大亮点
- NBUT 1462 台球碰撞 几何基础 思维
- 设计模式读书笔记之解释器模式(Interpreter pattern)
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
- static关键字的作用
- armeabi armeabi-v7a mips x86 理解
- UIViewController原理详解
- BoW图像检索Python实战
- 批量修改文件扩展名(批处理)
- 简易信用卡管理系统项目中常见的几个逻辑验证问题
- 053(四十六)
- ASP.NET中 RadioButtonList 单选按钮组控件的使用方法
- 未来属于SVO? - KlayGE游戏引擎
- Java学习-15天
- valse2016参会总结