(Array)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.
public class Solution { //更好的方法是排序,return 中间那个数
public int majorityElement(int[] nums) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i])+1);
} else
map.put(nums[i], 1);
}
Set set = map.entrySet();
Iterator it = set.iterator();
int res = 0;
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
if ((int) entry.getValue() > nums.length / 2)
res = (int) entry.getKey();
}
return res;
}
}
(Array)169. Majority Element的更多相关文章
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 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] ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
- 169. Majority Element - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 169. Majority Element (Array)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- python 字符串技巧 from python cookbook
所有数据进入程序中都只是一串字节 英文字符占一个字节 汉语是两个字节 一字节byte=8bit Unicode字符串为每种语言的每种字符设定了统一并且唯一的二进制编码 big = r'This is ...
- oracle select into 的时候提示未找到数据
); begin '; --在select into 后面添加exception 错误处理机制 exception when no_data_found then version:= 'hhh '; ...
- [1]开发准备-使用C#.NET开发基于本地数据缓存的PC客户端
小记:本人是PHPer,对C#.NET的开发只能说看得懂,也写得了功能略简单的PC客户端程序,下面的是本人开发一款名叫“理财速记”的PC客户端软件的全过程记录,期间包括比较繁琐的C#.NET资料查询等 ...
- ajax 页面加载
大体说说思路,不上代码了: 1.点击加载更多-> ajax向后台传参(当前页page,必须有默认1,其他需要的参数...) 2.后台接收 -> 查询数据 处理形成 json数据 给前台 3 ...
- RDD与DataFrame的转换
RDD与DataFrame转换1. 通过反射的方式来推断RDD元素中的元数据.因为RDD本身一条数据本身是没有元数据的,例如Person,而Person有name,id等,而record是不知道这些的 ...
- P1382 光棍组织
我现在TMD连dfs都不会写了 原题: MM 虽然一辈子只要一个,但是也得早点解决.于是,n 个光棍们自发组成了一个光棍组织(ruffian organization,By Wind 乱译).现在,光 ...
- TimeQuest 静态时序分析 基本概论
静态时序分析 基本概念 [转载] 1. 背景 静态时序分析的前提就是设计者先提出要求,然后时序分析工具才会根据特定的时序模型进行分析,给出正确是时序报告. 进行静态时序分析,主要目的就是为了提高 ...
- 将网页另存为PDF文件的方法
使用google chrome浏览器测试,其他浏览器应该也是差不多的方法. 步骤1: 打开需要转换的网页: 步骤2: 点击右上角的三点按键,或者快捷键Ctrl+P,调用的打印页面: 步骤3: 选择目标 ...
- redis之(二十一)redis之深入理解Spring Redis的使用
关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...
- Python之路,day3-Python基础
三级菜单 menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, ...