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的更多相关文章

  1. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  2. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  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 ...

  4. 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 ...

  5. 229. Majority Element II My Submissions Question

    Total Accepted: 23103 Total Submissions: 91679 Difficulty: Medium Given an integer array of size n, ...

  6. 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 ...

  7. [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 ...

  8. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  9. leetcode 229 Majority Element II

    这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...

  10. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...

随机推荐

  1. bzoj 2324: [ZJOI2011]营救皮卡丘

    #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #inclu ...

  2. POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0

    http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...

  3. HDU 3642 扫描线(立方体体积并)

    Get The Treasury Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. CodeForces 441E(Codeforces Round #252 (Div. 2))

    思路:dp[i][now][mark][len]   i 表示当前第i 次now存的是后8位,mark为第9位为0还是1 len第九位往高位还有几位和第9位相等.  只存后8位的原因:操作只有200次 ...

  5. fetch VS AJAX

    fetch('https://mywebsite.com/endpoint/', { method: 'POST', headers: { 'Accept': 'application/json', ...

  6. WPF 画刷应用

    纯色: SolidColorBrush brush = new SolidColorBrush(Colors.White); window1.Background = brush; 渐变色: Line ...

  7. SPOJ 10628 求树上的某条路径上第k小的点

    第k小,很容易会想到用主席树来解决 这里简单想一下树的转移过程 因为本身无向图形成一棵树,那么我们总以1为根,那么之后连下去的边对应的点建立的线段树总是在父亲节点对应的树上加上一个当前点对应位置出现的 ...

  8. Mac运行exe的几种方法,欢迎补充!

    1. 用wine直接运行exe.安装wine后有个放exe的文件夹,双击后会自动包装运行.看起来挺方便的,就怕暂用资源比较大: http://www.youtube.com/watch?v=eYISV ...

  9. <转>thinkphp自动验证无效的问题

    新手入门thinkphp,试用自动验证表单输入数据功能,却发现怎么都不能调用自动验证,自动验证无效,原因竟是一个小细节的疏忽,学习一定要细心啊! Action方法: IndexAction下的adds ...

  10. 理解Mac和iOS中的 Event 处理

    根据现在的理解,我把event处理分为5部分,第一是,Event处理的Architecture:第二是,Event的Dispatch到first responder之前: 第三是,Event从firs ...