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 ...
随机推荐
- springcloud Eureka自我保护机制
自我保护背景 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行. 默认情况下,如果Eureka Serve ...
- 用Executors工具类创建线程池
多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 线程池主要用来解决线程生命周期开销问题和资源不足问题.通过对多个任务重用线程,线程创建 ...
- jexus - 分析日志文件
1.统计IP访问次数 awk '{print $3}' default |sort -n|uniq -c|sort -rn|head
- springMVC:将controller中数据传递到jsp页面
1> 将方法的返回值该为ModelAndView在返回时,将数据存储在ModelAndView对象中如: newModelAndView("/WEBINF/jsp/showData.j ...
- node.js获取请求参数的方法和文件上传
var http=require('http') var url=require('url') var qs=require('querystring') http.createServer(onRe ...
- MVC底层原理
窥探ASP.Net MVC底层原理 实现跨越Session的分布式TempData 1.问题的引出 我相信大家在项目中都使用过TempData,TempData是一个字典集合,一般用于两个请求之间临时 ...
- 【AtCoder】Dwango Programming Contest V题解
A - Thumbnail 题意简述:给出N个数,找出N个数中和这N个数平均值绝对值最小的数 根据题意写代码即可= = #include <bits/stdc++.h> #define f ...
- Codeforces Round #533 (Div. 2) E - Helping Hiasat 最大团
E - Helping Hiasat 裸的最大团,写了一种 2 ^ (m / 2) * (m / 2)的复杂度的壮压, 应该还有更好的方法. #include<bits/stdc++.h> ...
- CSS------给字体添加边框时,边框大小无法改变问题
如图: 代码:(需要将display属性设置为inline-block,在设置height和line-height调整位置) //品牌点击 $(".li-brand").click ...
- 【Java】 int与char类型间的相互转化
在[Java] 剑指offer(16) 打印1到最大的n位数中遇到了int类型与char类型之间的转换,这里总结一下. (1)int类型转char类型,将数字加一个‘0’,并强制类型转换为char即可 ...