(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 ...
随机推荐
- NotePad++ 调试PHP代码中文显示乱码
最近在NotePad++上调试PHP代码,按照示例代码进行调试,结果在显示中文的时候显示一堆乱码,于是上网百度,有2种方法可以解决: 按调试方式有2种方法: 1.菜单插件-NppExec: " ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- python学习历程之split()方法获取cmd mysql 结果集
if __name__=='__main__': FServerId = raw_input("Please input source id:") GetFileKey(FServ ...
- 匈牙利算法 DFS模板(了解度+1)
//算法核心是求最大匹配数 #include<bits/stdc++.h> #include<iostream> #include<cstdio> #include ...
- cf 732c
/* cf732c 错过的最少次数 _________________________________________________________________________________ ...
- 一次失败的Selenium chromedriver切换
背景 Selenium webdriver一直使用Firefox作为浏览器来跑webtest, 但是最近发现ff有时会报超时的错误,于是想到使用chromedriver来提升稳定性.本想只把.fire ...
- tomcat跨域请求
tomcat A请求tomcat B中的数据(本人是在同一台机器上2个tomcat端口不同,在不同机器上有时间会测得) 博主用的是ajax请求 在A 请求B中数据,用下面的方法可以 在被请求的tomc ...
- Selenium 元素定位
selenium通过driver.findElement(By selector)来定位元素,selector在selenium-java.jar中,里面的方法一共就8种,如下图: 基本定义: By. ...
- 第二节:模型(Models)和管理后台(Admin site)
本节内容我们将配置数据库,创建第一个model并且快速了解Django自动生成的管理后台(admin site) 目录 数据库配置 创建模型 激活模型 使用Django API 介绍Django管理后 ...
- css布局课件
1.什么是CSS盒模型 我们的网页就是通过一个一个盒子组成的. 2.一个盒子拥有的属性:宽和高(width和height).内边距(padding).边框(border).外边距(margin) wi ...