Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

package cn.magicdu;

import java.util.HashMap;
import java.util.Map; public class _169_Majority_Element { /*public static void main(String[] args) {
_169_Majority_Element e=new _169_Majority_Element();
int arr[]={2,2,2,3,4,5,5,6,6,6,6,6,6,6,6};
System.out.println(e.majorityElement(arr));
}*/ public int majorityElement(int[] nums) {
Map<Integer,Integer> map=new HashMap<>();
int size=nums.length;
for(int i=0;i<size;i++){
if(map.containsKey(nums[i])){
map.put(nums[i],map.get(nums[i])+1);
}else{
map.put(nums[i],1);
}
if(map.get(nums[i])+1>size/2){
return nums[i];
}
}
return 0;
}
}

每天一道LeetCode--169.Majority Elemen的更多相关文章

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

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

  2. 23. leetcode 169. Majority Element

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

  3. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  4. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  8. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. Java for LeetCode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. JAVA实现HTTPserver端

    用java socket实现了一个简单的httpserver, 能够处理GET, POST,以及带一个附件的multipart类型的POST.尽管中途遇到了非常多问题, 只是通过在论坛和几个高手交流了 ...

  2. C#制作在线升级程序

    //这是一个webservice private AppUpdate.UpdateServ UpdateSvr; private void button1_Click(object sender, S ...

  3. On Memory Leaks in Java and in Android.

    from:http://chaosinmotion.com/blog/?p=696 Just because it's a garbage collected language doesn't mea ...

  4. /proc/sys/net/ipv4/下各项的意义

        /proc/sys/net/ipv4/icmp_timeexceed_rate这个在traceroute时导致著名的“Solaris middle star”.这个文件控制发送ICMP Tim ...

  5. 网口扫盲三:以太网芯片MAC和PHY的关系

    转载:http://www.cnblogs.com/jason-lu/articles/3195473.html   问:如何实现单片以太网微控制器? 答:诀窍是将微控制器.以太网媒体接入控制器(MA ...

  6. 阅读uboot

    下面是一个执行make XXX_config后的打印信息: pengdl@debian:~/work/costdown/new/Hi3520D_SDK_V1.0.2.2c/source/arm11/u ...

  7. Android(java)学习笔记82:我们到底该如何处理异常?

    我们到底该如何处理异常? 原则: 如果该功能内部可以将问题处理,用try,自己能解决就自己解决问题. 如果处理不了,交由调用者处理,这是用throws,自己不能解决的问题,我们就抛出去交个调用者解决这 ...

  8. Virtualbox - Fatal: Could not read from the boot medium; system halted!

    刚装好的虚拟机系统,重新打开Virtualbox时出现个什么提示没认真看,顺势点了下去......结果虚拟机的xp系统打不开了,启动后出现:Fatal: Could not read from the ...

  9. 5个让DBA爱上你的SQL技巧

    我的一个同事Martin Masarik,SQLde的CEO,跟我谈起了他的一个DBA朋友,他管理着一个国际银行的Oracle数据库,数据规模约2TB.Martin Masarik曾问他:“什么样的S ...

  10. SQL Server 调优:set statistics profile on

    进行set statistics profile on 设置后 将会返回执行计划表,通过该表,可以理解语句执行的过程,了解SQL Server是否选择了正确的执行计划,进而确定调优方向! 1.返回表字 ...