https://oj.leetcode.com/problems/majority-element/
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ times.
n/2 ⌋
You may assume that the array is non-empty and the majority element always exist in the array.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
public class Solution {
public int majorityElement(int[] num) {
int last=num[0];
int cnt=1;
for(int i=1;i<num.length;i++){
if(num[i]!=last){
cnt--;
if(cnt==0){
last=num[i];
cnt=1;
}
}else{
cnt++;
}
}
return last;
}
}
https://oj.leetcode.com/problems/majority-element/的更多相关文章
- leetcode https://oj.leetcode.com/problems/jump-game-ii/
1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- 【一天一道LeetCode】#169. Majority Element
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode Problem 169: Majority Element查找多数元素
描述:Given an array of size n, find the majority element. The majority element is the element that app ...
- 【刷题-LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/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 ...
- LeetCode【169. Majority Element】
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 如何方便的保存WinForm窗体控件的位置大小等等配置信息
由于分辨率.屏幕主题.字体大小的不同,窗体显示效果在不同机器上不尽相同.窗体的弹性设计并不能满足多样的需求.为保证在各种情况下,能有满意的效果.窗体的多样显示方式能改变,并且保存.载入配置,显得很重要 ...
- IT路上爹爹装装遇到的坑
1.中英文字符格式:UTF-8 有BOM和无BOM Notepad++ Edit-Plus emacs vim Sublime Text3 记事本 有,无 UTF-8+ #coding:utf-8 ...
- VC++ GetModuleFileName()获取路径字符串中带波浪线~
GetModuleFileName()获取的字符串中带波浪线,不是完整的路径显示. 原因:获取的是短路径,进行了缩写 解决:还原长路径 TCHAR }; GetLongPathName( strTem ...
- javaSE基础07
javaSE基础07 一.static静态修饰符 用了static修饰的变量就会变成共享的属性,只会初始化一次,在内存中只存在一个,并且每个对象都可以访问,存放在方法区(数据共享区) 1.1 stat ...
- 悬浮TabBar的实现--此段代码来自网络
悬浮TabBar的实现 这个TabBar看着像是用自定义TabBar做的,但事实上它还是用的系统的TabBar,给系统的tabBar.backgroundImage设置一张设计好的背景图片. TabB ...
- TFS API:一、TFS 体系结构和概念
TFS API:一.TFS 体系结构和概念 TFS是Team Fundation Server的简称,是微软VSTS的一部分,它是Microsoft应用程序生命周期管理(ALM)工具的核心协作平台, ...
- .NET Attribute 入门【笔记】
闲谈:没有系统的学过,偶尔看看,看的也不是很清楚. 却一直不明白,本来也不难,自己动手写了个示例.结果一目了然…… Attribute —— 标记 可以对方法.类等事务进行附着.增加属性. 下面以我见 ...
- 【转载】兼容php5,php7的cURL文件上传示例
转载来自: http://www.huanlinna.com/2016/06/25/coding/php5-php7-upload-demo-via-curl.html https://segment ...
- Laravel5路由/home页面无法访问
报错信息: Not Found The requested URL /laravel5/public/home was not found on this server. 解决方法: 1.编辑apac ...
- Spring集成JUnit测试
1.程序中有Junit环境2.导入一个jar包.spring与junit整合jar包 spring-test-3.2.0.RELEASE.jar3.测试代码 @RunWith(SpringJUnit4 ...