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.

Hide Tags: Divide and Conquer Array Bit Manipulation

解题思路:

(1)使用HashMap。Map的特点:不同意反复元素,因此在存储前须要推断是否存在

(2)推断HashMap中存在nums[i],假设存在。使用hm.get(nums[i])获取value,即通过key来获得value值,即count(出现的次数)

(3)假设count大于数组长度的一般。即返回该元素

(4)假设count不满足条件,向HashMap存储元素以及出现的次数。

代码例如以下:

	public static int majorityElement(int[] nums)
{
/*
* Map的特点:不同意反复元素。因此在存储前须要推断是否存在
*/
Map<Integer, Integer> hm=new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++)
{
int count=0;
//推断HashMap中存在nums[i],假设存在,使用hm.get(nums[i])获取value
//即通过key来获得value值,即count(出现的次数)
if (hm.containsKey(nums[i]))
{
count=hm.get(nums[i])+1;
}
else
{
count=1;
}
//假设count大于数组长度的一般,即返回该元素
if (count>nums.length/2)
{
return nums[i];
}
//向HashMap存储元素以及出现的次数
hm.put(nums[i], count);
}
return 0; }

leetcode——169 Majority Element(数组中出现次数过半的元素)的更多相关文章

  1. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  2. LINQ 获取当前数组中出现次数最多的元素

    LINQ 获取当前数组中出现次数最多的元素 1  List<string> a = new List<string>();              a.Add(        ...

  3. python查找数组中出现次数最多的元素

    方法1-np.argmax(np.bincount()) 看一个例子 array = [0,1,2,2,3,4,4,4,5,6] print(np.bincount(array)) print(np. ...

  4. 23. leetcode 169. Majority Element

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

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

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

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

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

  7. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  8. LeetCode 169. Majority Element解题方法

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

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

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

随机推荐

  1. hibernate介绍及环境搭建

    1.前言 hibernate与mybatis的位置一样,都是属于DAO层的框架,代替我们原来的JDBC操作数据库,属于ORM(object relationg mapping. 对象关系映射)框架.O ...

  2. 【比赛游记】NOIP2017游记

    身为FJ的选手,在师大附中AHSOFNU考试,环境很不错,考得也还可以吧...[考的并不好] 不过比赛前都在划水233333 另:看到这篇博客的OIer们一定要评论啊! Day1的中午,因为穿了短袖去 ...

  3. VMware下centos桥接模式静态ip配置

    声明:本文转载自http://blog.csdn.net/ltr15036900300/article/details/48828207,非原创. 一.配置虚拟机centos网络 备份网络文件 [ro ...

  4. SpringBoot修改默认端口号,session超时时间

    有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...

  5. 不使用第三方软件、使用IE11自带功能来屏蔽浏览器广告

    第一步: 下载后面的附件http://files.cnblogs.com/limits/IE11%E5%8E%BB%E5%B9%BF%E5%91%8A.zip 打开此路径IE11跟踪保护+CSS去广告 ...

  6. Windwos8.1下配置PHP环境

    一.     下载安装包: Apache2.2:http://mirrors.cnnic.cn/apache//httpd/binaries/win32/httpd-2.2.25-win32-x86- ...

  7. Sublime Text 中open in browser /view in browser 无反应

    问题 早上用Sublime Text写html的时候,发现右键的open in browser或view in browser命令都突然无法使用了,无法像以前一样在浏览器打开编写的页面了. 开始以为是 ...

  8. 如何安装pycharm

    Ubuntu系统安装PyCharm教程(详细图文) 参考(http://jingyan.baidu.com/article/60ccbceb4e3b0e64cab19733.html)  

  9. Angular 2的表格控件

    Angular 2的表格控件 前端框架一直这最近几年特别火的一个话题,尤其是Angular 2拥有众多的粉丝.在2016年9月份Angular 2正式发布之后,大量的粉丝的开始投入到了Angular ...

  10. 【noip模拟赛3】确定的位置 (map的遍历 位置原理)

    描述 hzy很喜欢了解歌曲的排行榜,他每次都从XX网站获知. 由于这个网站想对这个歌曲的排行榜含蓄的告诉大家,组织了一个“猜榜大赛”. 这个网站宣布一些歌曲的信息,那些歌曲在歌曲榜上的前几名 例如: ...