(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 ... 
随机推荐
- xss跨站脚本测试
			测试的时候会涉及到xss测试,下面简要整理下xss的知识 xss跨站脚本特点就是能注入恶意的HTML/JS代码到用户浏览器,劫持用户会话 常用alert来验证网站存在漏洞 如果确认存在漏洞,会随着注入 ... 
- VTK的学习资源
			本文介绍从哪儿开始学习VTK(Visualization Toolkit的简称),如何在网上找寻VTK的学习资源. 首先,可以到维基百科或者百度百科上查看VTK条目,了解VTK是什么. http:// ... 
- SqlServer 行转列(统计某年一到十二个月数据总和)
			select * from( select sum(case MONTH(purchase_date) when '1' then SumMoney else 0 end) as January ... 
- AFNetworking 3.0
			AFN 一.什么是AFN 全称是AFNetworking,是对NSURLConnection的一层封装 虽然运行效率没有ASI高,但是使用比ASI简单 在iOS开发中,使用比较广泛 AFN的githu ... 
- 带日期的bean转为json(bean->JSON)
			示例代码: JsonBean bean = new JsonBean();bean.setName("NewBaby");bean.setAge(1);bean.setBorn(n ... 
- Android——黑名单
			<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ... 
- JS-学习-DOM元素尺寸和位置
			一,获取元素的css大小 1.通过style内联获取元素的大小 var box = document.getElementById('box'); // 获得元素; box.style. ... 
- 堆排序(c++第一次尝试)
			对排序的实现思路有两种 第一种:1.构建最小堆.2.将最小堆的堆顶元素取出放到辅助数组的0号下标.3.重新调整成最小堆(向上调整) 4.重复2-3 第二种:1.构建最大堆.2.将堆顶元素(0号)与最后 ... 
- OpenStack Mitaka安装
			http://egon09.blog.51cto.com/9161406/1839667 前言: openstack的部署非常简单,简单的前提建立在扎实的理论功底,本人一直觉得,玩技术一定是理论指导实 ... 
- MyBatis学习(二)
			前言 昨天的博客简单的记录了MyBatis的起源.作用.配置以及一个简单的查询例子.写到一半的时候,觉得已经学会了MyBatis,可是全写完的时候才发现,如果多个参数查询,如何表的名字与类字段名不一样 ... 
