leetcode——169 Majority Element(数组中出现次数过半的元素)
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(数组中出现次数过半的元素)的更多相关文章
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
- LINQ 获取当前数组中出现次数最多的元素
LINQ 获取当前数组中出现次数最多的元素 1 List<string> a = new List<string>(); a.Add( ...
- python查找数组中出现次数最多的元素
方法1-np.argmax(np.bincount()) 看一个例子 array = [0,1,2,2,3,4,4,4,5,6] print(np.bincount(array)) print(np. ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- LeetCode 169 Majority Element 解题报告
题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...
- LeetCode 169. Majority Element解题方法
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 20165227 2017-2018-2《Java程序设计》课程总结
20165227 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1 简要内容: 记忆深刻的老师 我期望的师生关系 对于Java学习的看法 预备作业2 简要内 ...
- VCForPython27.msi安装后, 还显示error: Unable to find vcvarsall.bat
C:\Users\zpc\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC 增加环境变量: SET VCPYTH ...
- shell expect的简单用法【转】
用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理. 脚本代码如下: ######## ...
- mitmproxy实践
首先附上github地址:https://github.com/mitmproxy/mitmprox,上面的内容永远是最新的 作为一名测试穿戴设备相关app的工程师,与数据打交道是常事,那么,如果想要 ...
- 基于Apache在本地配置多个虚拟主机站点
简单的说,打开httpd.conf 在最后加入如下内容: <VirtualHost 127.0.0.2:80> DocumentRoot d:/AppServ/www2 Ser ...
- Extjs Window用法详解 3 打印具体应用,是否关掉打印预览的界面
Extjs Window用法详解 3 打印具体应用,是否关掉打印预览的界面 Extjs 中的按钮元素 {xtype: 'buttongroup',title: '打印',items: [me.ts ...
- excel 2016 for mac破解
1: 首先去官网下载一个正版的: 2:再下载一个破解工具: 链接: http://pan.baidu.com/s/1i4AFHFf 密码: 3yf8 3:最后按照破解教程破解: http://jing ...
- SysV和BSD启动风格的比较
Slackware启动脚本与System V启动脚本的区别何在? Slackware 使用BSD风格的init脚本,而很多别的发行版使用System V风格的init脚本.SysV和BSD脚本都是能让 ...
- java 编译与运行
javac 编译 .java文件 javac file.java //将file.java 编译为 file.classjavac -d folder file.java //将file.java ...
- 解决MySQL忘记root密码
网上有很多关于忘记MySQL root密码的一些文章,里面都有写怎么去解决,但有时觉得写得太恶心,要么一字不漏的抄别人的,要么就说得不清不楚,好了,不吐槽了,以下是解决的整个过程. 首先我们要知道忘记 ...