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 ...
随机推荐
- java中byte的范围计算
概念:java中用补码表示二进制数,补码的最高位是符号位,最高位为“0”表示正数,最高位为“1”表示负数.正数补码为其本身:负数补码为其绝对值各位取反加1:例如:+21,其二进制表示形式是000101 ...
- 执行Commit时Oracle做哪些工作
COMMIT是一个非常快的操作,当我们发布commit命令时,真正困难的动作已经完成,在数据库中已经执行了数据更改,所以已经完成了99%的任务,例如:下列操作已经产生: 1.在SGA(Buffer C ...
- Django中出现:TemplateDoesNotExist at
setting文件中加入:
- 基于JS的高级脚本语言 Sara
Sara-基于JS的高级脚本语言 欢迎使用Sara,Sara是一款基于JavaScript的全新的高级脚本语言! Sara不像我们工作室上一款编程语言作品-Ginit一样,他属于更高级的语言 Sara ...
- idea插件开发
用过的group ProjectViewPopupMenu 项目.目录的右键弹出菜单 EditorPopupMenu 编辑器里面的右键弹出菜单 调出右键菜单的关键字 show context menu
- GeoJSON与GeoBuf互相转换
GeoJSON格式通常比较大,网页需要较长时间加载,可以使用GeoBuf进行压缩. 使用GeoBuf有很多好处:结构紧凑.文件小.方便编码和解码.能适用各种GeoJSON等等. 使用: 1.安装 ge ...
- php常量和变量之变量引用
变量引用 变量引用很多老师喜欢来用C语言的指针来去讲解.我们作为有这么多年开发和教学经验的人来说——大多数学习PHP的人来说根本不了解C语言. 使用C语言一指针来讲解变量引用,我们觉得画蛇填足.并且, ...
- 007——转载-MATLAB读取文件夹下的文件名
(一)参考文献:https://blog.csdn.net/liutaojia/article/details/84899923 (二)第一步:获取文件夹下某类型数据的所有文件名 主要包括三个步骤: ...
- 利用亚马逊AWS搭建个人服务器
转载博客地址:https://www.jianshu.com/p/a045d4217175
- [转] C++ STL中map.erase(it++)用法原理解析
总结一下map::erase的正确用法. 首先看一下在循环中使用vector::erase时我习惯的用法: for(vector<int>::iterator it = vecInt.be ...