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. VMware下利用ubuntu13.04建立嵌入式开发环境之四

    二.telnet.SSH服务器安装与配置 1.telnet 1.1 安装服务器:apt-get install xinetd telnetd 1.2 安装openbsd-inetd:apt-get i ...

  2. 写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”)

    public class Test { public void index() { String strWords = "Hello World My First Unit Test&quo ...

  3. JAVA学习心得

    学习JAVA一个学期了,虽然学的不是很好,也没有彻底理解JAVA的真正内涵,但是我认为学习java应该是循环渐进,有始有终,按部就班,脚踏实地.java是一门有着阶梯性的一门语言,信心,恒心,毅力是最 ...

  4. 关于JQuery简单介绍

    jQuery是一个兼容多浏览器的javascript库,核心理念是写得更少,做得更多.如今,jQuery已经成为最流行的javascript库,在世界前10000个访问最多的网站中,有超过55%在使用 ...

  5. Spark源码学习1.6——Executor.scala

    Executor.scala 一.Executor类 首先判断本地性,获取slaves的host name(不是IP或者host: port),匹配运行环境为集群或者本地.如果不是本地执行,需要启动一 ...

  6. Spark源码学习1.4——MapOutputTracker.scala

    相关类:MapOutputTrackerMessage,GetMapOutputStatuses extends MapPutputTrackerMessage,StopMapOutputTracke ...

  7. oracle数据类型

    本文转自:http://blog.csdn.net/defonds/article/details/4302695 谢谢原文作者 有道是,磨刀不误砍柴工.多了解一些底层的东西,对于Oracle开发.维 ...

  8. java语法基础

    Java的基本符号(token) Java的单词符号有五种:关键字.标识符.常量.分隔符和操作符. Java的字符集 Java 采用一种称为unicode的字符集,该字符集合是一种新的编码标准,与常见 ...

  9. Varnish常用相关命令工具

    varnishd启动./varnishd  -f /usr/local/varnish-3.0.2/etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1: ...

  10. VS2012新建项目出错:未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService

    刚刚重新做的系统,第一次打开vs2012新建项目时出现错误提示 通过查找解决办法发现方法有两种: 1:卸载两个windows更新补丁(KB2833957和KB2840642) 2:安装一个window ...