LeetCode_169. Majority Element
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.
Example 1:
Input: [3,2,3]
Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2
package leetcode.easy; import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random; public class MajorityElement {
@org.junit.Test
public void test() {
int[] nums1 = { 3, 2, 3 };
int[] nums2 = { 2, 2, 1, 1, 1, 2, 2 };
System.out.println(majorityElement1(nums1));
System.out.println(majorityElement1(nums2));
System.out.println(majorityElement2(nums1));
System.out.println(majorityElement2(nums2));
System.out.println(majorityElement3(nums1));
System.out.println(majorityElement3(nums2));
System.out.println(majorityElement4(nums1));
System.out.println(majorityElement4(nums2));
System.out.println(majorityElement5(nums1));
System.out.println(majorityElement5(nums2));
System.out.println(majorityElement6(nums1));
System.out.println(majorityElement6(nums2));
} public int majorityElement1(int[] nums) {
int majorityCount = nums.length / 2; for (int num : nums) {
int count = 0;
for (int elem : nums) {
if (elem == num) {
count += 1;
}
} if (count > majorityCount) {
return num;
}
} return -1;
} private Map<Integer, Integer> countNums(int[] nums) {
Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
for (int num : nums) {
if (!counts.containsKey(num)) {
counts.put(num, 1);
} else {
counts.put(num, counts.get(num) + 1);
}
}
return counts;
} public int majorityElement2(int[] nums) {
Map<Integer, Integer> counts = countNums(nums); Map.Entry<Integer, Integer> majorityEntry = null;
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
if (majorityEntry == null || entry.getValue() > majorityEntry.getValue()) {
majorityEntry = entry;
}
} return majorityEntry.getKey();
} public int majorityElement3(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
} private int randRange(Random rand, int min, int max) {
return rand.nextInt(max - min) + min;
} private int countOccurences(int[] nums, int num) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == num) {
count++;
}
}
return count;
} public int majorityElement4(int[] nums) {
Random rand = new Random(); int majorityCount = nums.length / 2; while (true) {
int candidate = nums[randRange(rand, 0, nums.length)];
if (countOccurences(nums, candidate) > majorityCount) {
return candidate;
}
}
} private int countInRange(int[] nums, int num, int lo, int hi) {
int count = 0;
for (int i = lo; i <= hi; i++) {
if (nums[i] == num) {
count++;
}
}
return count;
} private int majorityElementRec(int[] nums, int lo, int hi) {
// base case; the only element in an array of size 1 is the majority
// element.
if (lo == hi) {
return nums[lo];
} // recurse on left and right halves of this slice.
int mid = (hi - lo) / 2 + lo;
int left = majorityElementRec(nums, lo, mid);
int right = majorityElementRec(nums, mid + 1, hi); // if the two halves agree on the majority element, return it.
if (left == right) {
return left;
} // otherwise, count each element and return the "winner".
int leftCount = countInRange(nums, left, lo, hi);
int rightCount = countInRange(nums, right, lo, hi); return leftCount > rightCount ? left : right;
} public int majorityElement5(int[] nums) {
return majorityElementRec(nums, 0, nums.length - 1);
} public int majorityElement6(int[] nums) {
int count = 0;
Integer candidate = null; for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
} return candidate;
}
}
LeetCode_169. Majority Element的更多相关文章
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- (Array)169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
- Leetcode # 169, 229 Majority Element I and II
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode【169. Majority Element】
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 一、XML DOM、XMLDocument
一.XML DOM概述 XML 文档大小写敏感.属性用引号括起来,每一个标记都要闭合. DOM是XML文档的内存中树状的表示形式. 继承关系图: XmlNode;//XML节点 ......XmlDo ...
- Listener中@Autowired无法注入的问题
最近在用监听器的时候遇到了spring无法注入的问题,代码如下,这个task总是null,包明明已经被扫到了,就是注入不进来. public class MyListener implements S ...
- postgresql学习笔记--基础篇 - copy
1. psql 导入/导出数据 psql支持文件数据导入到数据库,也支持数据库表数据导出到文件中. COPY命令和\copy 命令都支持这两类操作,但两者有如下区别: COPY 命令是SQL命令,\c ...
- MySQL 表查询
表查询 前期准备一张表 create table emp( id int not null unique auto_increment, name varchar(32) not null, gend ...
- 有关求第n位xxx 的算法的问题
最近,博客园上看到有关求 斐波那契数列的第n位是什么的问题.什么是 斐波那契数列? 我自己也忘记了,后来百度了下.http://baike.baidu.com/view/816.htm?fr=alad ...
- HTML 文字剧中
HTML 内想使文字剧中的办法 就是 text-align:center 剧中前效果图 剧中后效果图 代码:
- spring security控制session
spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...
- 六十一.常用组件 、 Kafka集群 、 Hadoop高可用
1.Zookeeper安装搭建Zookeeper集群并查看各服务器的角色停止Leader并查看各服务器的角色 1.1 安装Zookeeper1)编辑/etc/hosts ,所有集群主机可以相互 pin ...
- bzoj 3721: PA2014 Final Bazarek 贪心
如果没有限制,直接取前 $k$ 大即可. 有限制,则只有几种可能:奇换偶,偶换奇. 维护奇数偶数的前缀最小值和后缀最大值即可. code: #include <bits/stdc++.h> ...
- Linux操作系统常用命令合集——第二篇- 用户和组操作(15个命令)
一.前言:本篇介绍用户和组操作的15个命令,在介绍之前我们先来看看几个示例 1.先进入到etc目录下,找到passwd文件,用vi编辑器查看: # vi /etc/passwd 解释:这里面存放着Li ...