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& ...
随机推荐
- 安装Adobe Dreamweaver CS6 免序列号 官方破解版
Adobe Dreamweaver CS6 免序列号 官方破解版 Adobe Dreamweaver CS6是世界顶级软件厂商Adobe推出的一套可视化的网页开发工具,Dreamweaver CS6最 ...
- 一模 (6) day1
第一题: 题目大意: 一个n的全排列A[i]是单峰的,当且仅当存在某个x使得A[1]<A[2]<...<A[x]>A[x+1]>...>A[n]. 试求 n 的单峰 ...
- svnadmin:error while loading shared libraries: libaprutil-1.so.0:cannot open shared object file: No such file or directory
wdcp下安装svn后一直提示 svnadmin:error while loading shared libraries: libaprutil-1.so.0:cannot open shared ...
- Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)
Java中实现多线程的方法有两种: 继承Thread类和实现Runnable方法,并重写Run方法,然后调用start()方法启动线程.使用Runnable会比Thread要好很多,主要是以下三个原因 ...
- Problem B 队列
Description Two bored soldiers are playing card war. Their card deck consists of exactly n cards, nu ...
- println与toString()
public class Test{ public static void main(String[] args) { Mankind mk=new Mankind(); System.out.p ...
- mysql 导入数据库文件到指定数据库
i:\mysql\bin>mysql -u 用户名 -p 数据库名 < i:/test.sql // (source "c:\adsense.sql" ) ...
- Windows 技术预览版 - 传言中的Win 10
http://windows.microsoft.com/zh-cn/windows/preview-iso Windows Technical Preview 产品密钥: NKJFK-GPHP7-G ...
- ISO c++11 does not allow conversion from string literal to 'char*'
http://stackoverflow.com/questions/9650058/deprecated-conversion-from-string-literal-to-char
- HTTP协议详解(经典)
转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的 ...