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.

方法很笨,其实时间复杂度差不多是O(n²)。

package leetcode;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
//import java.util.Scanner;
import java.util.Set;
public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<Integer>();
int n = nums.length;
int m = n/3;
Set<Integer> myset= new HashSet<Integer>();
for(int i=0;i<n;i++)
{
myset.add(nums[i]);
}
Iterator<Integer> iterator = myset.iterator();
while(iterator.hasNext())
{
int thisnum = iterator.next();
int count = 0;
for(int i=0;i<n;i++)
{ if(thisnum == nums[i])
{
count++;
if(count>m)
{
result.add(thisnum);
break;
}
}
}
}
return result; }
public static void main(String[] args)
{
Solution sl = new Solution();
int[] nums = {2,2,4,6,7,4,2,4};
List<Integer> result = sl.majorityElement(nums);
System.out.println(result); }
}

Majority Element II的更多相关文章

  1. LeetCode(169)Majority Element and Majority Element II

    一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...

  2. Majority Element,Majority Element II

    一:Majority Element Given an array of size n, find the majority element. The majority element is the ...

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

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

  4. Majority Element(169) && Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...

  5. 【LeetCode】229. Majority Element II

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

  6. LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III

    LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...

  7. 【刷题-LeetCode】229. Majority Element II

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

  8. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  9. Leetcode: Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

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

随机推荐

  1. J2EE、J2SE、J2ME是什么意思?

    本文介绍Java的三大块:J2EE.J2SE和J2ME.J2SE就是Java2的标准版,主要用于桌面应用软件的编程:J2ME主要应用于嵌入是系统开发,如手机和PDA的编程:J2EE是Java2的企业版 ...

  2. .NET/android/java/iOS AES通用加密解密(修正安卓)

    移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如.NET和android或者iOS的打交道.为了让数据交互更安全,我们需要对数据进行加密传输.今天研究了一下,把几种语言的加密都 ...

  3. Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] --11061188刘强

    结对编程总结 队员:刘强(11061188) 林谋武(11061169) 结对编程: 结对编程的优点: 1.  两个人合作,相比于一个人自己奋斗而言,更能激发自己的潜能:我们在合作过程中,互相学习,互 ...

  4. 【python】操作excel——xlrd xlwt xlutils

    from xlutils.copy import copy import xlrd # import xlutils #打开已存在的excel rb=xlrd.open_workbook('D:\\1 ...

  5. SWFUpload - JQuery上传插件

    首先,大家可以去SWF的官网下载相关Demo或者源码. 官方地址:http://code.google.com/p/swfupload/ 官方Demo:http://demo.swfupload.or ...

  6. hdu 2062

    ps:11版的最后一题...是个递推题...比如n=5,推出首数字后,n--,继续找下一个 代码: #include "stdio.h" ]; ]; int main(){ lon ...

  7. 长时间停留在calculating requirements and dependencies 的解决方案

    如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...

  8. 每天学习一点点--word-break和word-wrap用法和区别

    有时候一个又臭又长的单词出现在一个并不宽到足以容纳这个单词时会出现内容溢出容器这种情况: <!DOCTYPE html> <html lang="en"> ...

  9. redis 数据类型详解 以及 redis适用场景场合

    1.  MySql+Memcached架构的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的 ...

  10. _crol_和_cror_函数

    “_crol_” 与“_cror_”其实就是左右循环代码,其具有程序代码简单执行效率高的优点! 是在单片机c语言编程中常用到的,变量=_crol_(变量名,移动位数),例如:P0=_crol_(P0, ...