(medium)LeetCode 229.Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
解法:参考编程之美129页,寻找发帖水王
代码如下:
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer>list=new ArrayList<Integer>();
if(nums==null) return list;
int len=nums.length;
if(len==0) return list;
if(len==1){
list.add(nums[0]);
return list;
}
int m1=nums[0];
int m2=0;
int c1=1;
int c2=0;
for(int i=1;i<len;i++){
if(nums[i]==m1)
c1++;
else if(nums[i]==m2)
c2++;
else if(c1==0){
m1=nums[i];
c1=1;
}else if(c2==0){
m2=nums[i];
c2=1;
}
else{
c1--;
c2--;
}
}
c1=0; c2=0;
for(int i=0;i<len;i++){
if(nums[i]==m1)
c1++;
else if(nums[i]==m2)
c2++;
}
if(m1!=m2){ //防止[0,0]情况出现,其实不加也可以,加了运行时间缩短20ms
if(c1>len/3) list.add(m1);
if(c2>len/3) list.add(m2);
}else{
list.add(m1);
}
return list;
}
}
运行结果:
(medium)LeetCode 229.Majority Element II的更多相关文章
- [LeetCode] 229. Majority Element II 多数元素 II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- leetcode 229 Majority Element II
这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...
- 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 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...
- Java for 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 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 【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】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
随机推荐
- js和css内联外联注意事项
简单说:这两个问题其实是同一个问题,但是网上找了好久也找不到方法,外联的js和css文件里不能有任何HTML的标记注释,一旦有,浏览器就疯了!一去掉就好了!!! 问题:起因是网上看到一个css的表格样 ...
- 当当网开源Dubbox,扩展Dubbo服务框架支持REST风格远程调用
当当网近日开源了Dubbox项目,可为Dubbo服务框架提供多项扩展功能,包括REST风格远程调用.Kryo/FST序列化等等. 当当网架构部和技术委员会架构师沈理向InfoQ中文站介绍了Dubbox ...
- WebLogic11g-半小时让你的domain集群化
WebLogic11g-半小时让你的domain集群化 WebLogic11g-负载分发 weblogic proxy.war配置 web.xml <!DOCTYPE web-app PUBLI ...
- [摘]Hibernate查询事务必要性
背景: 添加事务与否都不影响Hibernate的查询操作. 问题: 查询操作是否有必要添加事务? 答案1: Hibernate官方手册上建议任何操作(增删改查)都需要添加事务. 答案2: robbin ...
- JQUERY 拖拽 draggable droppable resizable selectable sortable
今天用了jq ui的拖动碰撞功能,好不容易看到有详细的API解说,记录如下: <script language="JavaScript" type="text/ ...
- 提示“应用程序无法启动,因为应用程序的并行配置不正确”不能加载 System.Data.SQLite.dll
新版本SQLITE,如果下载Precompiled Binaries版会出现提示“应用程序无法启动,因为应用程序的并行配置不正确”不能加载 System.Data.SQLite.dll. 下载Prec ...
- Tomcat服务器搭建
一.JDK环境搭建 二.tomcat下载安装 三.tomcat服务启动 cmd> net start tomcat8 四.查看tomcat服务器启动情况: http://localhost:8 ...
- angularjs中ng-route和ui-router简单用法的代码比较
1.使用ng-route: app.js中的写法: var app=angular.module('birthdayApp',['ngRoute']); app.config(function($ro ...
- visual studio 声明数组太大,导致栈溢出
在解释原因前我们先看一下一个由C/C++编译的程序占用的内存分为几个部分: 1.栈区(stack segment):由编译器自动分配释放,存放函数的参数的值,局部变量的值等.在Windows下,栈是向 ...
- Git-Flow
Overview Git-Flow is a high-level command set wrapping low-level Git commands to support the "s ...