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.
代码如下:(方法一:超内存)
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> list=new ArrayList<>();
if(nums.length==0)
return list;
Arrays.sort(nums);
int[] a=new int[nums[nums.length-1]+1];
for(int i=0;i<nums.length;i++)
a[nums[i]]++;
for(int i=0;i<nums.length;)
{
if(a[nums[i]]>nums.length/3)
{
list.add(nums[i]);
i=i+a[nums[i]]-1;
}
}
return list;
}
}
方法二:(借鉴别人的)
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> list=new ArrayList<>();
if(nums.length==0)
return list;
if(nums.length==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<nums.length;i++)
{
if(m1==nums[i])
c1++;
else if(m2==nums[i])
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<nums.length; i++) {
if(m1 == nums[i]) ++c1;
else if(m2 == nums[i]) ++c2;
}
if(c1>nums.length/3)
list.add(m1);
if(c2>nums.length/3)
list.add(m2);
return list;
}
}
229. Majority Element II的更多相关文章
- 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 ...
- 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 229. Majority Element II My Submissions Question
Total Accepted: 23103 Total Submissions: 91679 Difficulty: Medium Given an integer array of size n, ...
- LeetCode OJ 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 多数元素 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- leetcode 229 Majority Element II
这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...
随机推荐
- Union all的用法实例sql
---Union all的用法实例sqlSELECT TOP (100) PERCENT ID, bid_user_id, UserName, amount, createtime, borrowTy ...
- MSMQ消息队列
MSMQ全称MicroSoft Message Queue,微软消息队列,是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一 ...
- windbg调试C#代码(一)
用windbg调试C#代码是比较麻烦的,因为windbg是针对OS层级的,而C#被CLR隔了一层,很多原生的命令如查看局部变量dv.查看变量类型dt等在CLR的环境中都不能用了.必须使用针对CLR的扩 ...
- POJ 2763
题意:给一个数,边之间有权值,然后两种操作,第一种:求任意两点的权值和,第二,修改树上两点的权值. #pragma comment(linker, "/STACK:1024000000,10 ...
- JSONObject put,accumulate,element的区别(转载)
原文链接:http://ljhzzyx.blog.163.com/blog/static/3838031220126810430157/ public Object put (Object key ...
- APC to USB
from :http://www.allpinouts.org/index.php/APC_USB_cable_schematic connector or cable wiring APC part ...
- C/C++中函数参数传递详解(一)
*在定义时使用代表指针类型,其他情况代表取内容.&在定义时使用代表引用(别名),在其他情况代表取地址 在编写个人函数的时候,你将会受到C++中的一条基本的原则的限制:在默认的情况下,变量只能以 ...
- Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(一)
MEX文件是一种可在matlab环境中调用的C语言(或fortran)衍生程序,mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件. 中文名 mex文件 外文名 MATLAB ...
- redis——基础介绍
转自:http://www.cnblogs.com/xing901022/p/4863929.html 1 什么是Redis Redis(REmote DIctionary Server,远程数据字典 ...
- spark与Hadoop区别
2分钟读懂Hadoop和Spark的异同 2016.01.25 11:15:59 来源:51cto作者:51cto ( 0 条评论 ) 谈到大数据,相信大家对Hadoop和Apache Spark ...