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 ...
随机推荐
- 让一个图片在div中居中(四种方法)
第一种方法: <div class="title"> <div class="flag"></div> <div cl ...
- 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
实验目的 掌握在 ARM 开发板实现一个简单 WEB 服务器的过程. 学习在 ARM 开发板上的 SOCKET 网络编程. 学习 Linux 下的 signal()函数的使用. 实验内容 学习使用 s ...
- Linux-详解inode节点
Linux inode节点 inode查看命令 stat 功能:列出文件大小,文件所占的块数,块的大小,主设备号和次设备号,inode number,链接数,访问权限,uid,gid,atime,mt ...
- MFC如何读取XML
<?xml version="1.0" encoding="utf-8"?> <Cases> <case> <No&g ...
- python中date、datetime、string的相互转换
import datetime import time string转datetime str = '2012-11-19' date_time = datetime.datetime.strptim ...
- for_each(c++11)
http://www.cplusplus.com/reference/algorithm/for_each/ template<class InputIterator, class Functi ...
- Lintcode 469. 等价二叉树
----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...
- NotePad++中如何出去闪烁的光标?
当在写代码时出现的光标闪烁(横线闪烁),在键盘上找 Insert ,按这个Insert就可以把横向闪烁光标( _ )修改成竖向闪烁光标样式( | ),横向光标会在你写代码的时候修改前面的代码,把光标移 ...
- python ImportError: No module named builtins
解决方案: sudo pip install future
- ppmoney
build/config.js 改 8080端口 build/webpack.dev.conf.js 改路径简写 alias:{ 'vux-components':'vux/dist/componen ...