Leet Code OJ 169. Majority Element [Difficulty: Easy]
来源:互联网 发布:罗伊数据 编辑:程序博客网 时间:2024/05/02 04:31
题目:
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.
思路分析:
题意是给定一个长度为n的数组,找出“主要元素”。“主要元素”指的是出现次数大于⌊ n/2 ⌋的元素。
代码实现:
public class Solution { public int majorityElement(int[] nums) { int point = nums.length / 2; Map<Integer, Integer> map = new HashMap<>(); for (int num : nums) { Integer value = map.get(num); if (value == null) { value = 1; } else { value++; } map.put(num, value); if (value > point) { return num; } } return 0; }}
1 0
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
- Leet Code OJ 27. Remove Element [Difficulty: Easy]
- Majority Element (Leet Code)
- Leet Code OJ 292. Nim Game [Difficulty: Easy]
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
- Leet Code OJ 231. Power of Two [Difficulty: Easy]
- Leet Code OJ 258. Add Digits [Difficulty: Easy]
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
- Leet Code OJ 283. Move Zeroes [Difficulty: Easy]
- Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]
- Leet Code OJ 263. Ugly Number [Difficulty: Easy]
- Leet Code OJ 100. Same Tree [Difficulty: Easy]
- Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
- Leet Code OJ 202. Happy Number [Difficulty: Easy]
- Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
- Leet Code OJ 171. Excel Sheet Column Number [Difficulty: Easy]
- Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]
- centos7常用工具软件安装
- Android自定义带下划线的TextView
- Python2.7 urlparse学习
- C语言学习笔记1-数据类型和标识符
- SD机auto mount自动挂载文件系统
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
- 有感于东野圭吾小说
- hibernate相关
- MySql导出导入表和数据
- C++基础::string(三)
- 食物链(并查集)
- win8不能安装mysql
- KVC&&KVO
- 使用 WSDL 部署 Web 服务: 第 1 部分