Majority Element 解答
Solution 1
Naive way
First, sort the array using Arrays.sort in Java. Than, scan once to find the majority element. Time complexity O(nlog(n))
public class Solution {
public int majorityElement(int[] nums) {
int length = nums.length;
if (length == 1)
return nums[0];
Arrays.sort(nums);
int prev = nums[0];
int count = 1;
for (int i = 1; i < length; i++) {
if (nums[i] == prev) {
count++;
if (count > length / 2) return nums[i];
} else {
prev = nums[i];
count = 1;
}
}
return 0;
}
}
Solution 2
Since the majority always take more than a half space, the middle element is guaranteed to be the majority.
public class Solution {
public int majorityElement(int[] nums) {
int length = nums.length;
if (length == 1)
return nums[0];
Arrays.sort(nums);
return nums[length / 2];
}
}
Solution 3 Moore voting algorithm
As we iterate the array, we look at the current element x:
- If the counter is 0, we set the current candidate to x and the counter to 1.
- If the counter is not 0, we increment or decrement the counter based on whether x is the current candidate.
After one pass, the current candidate is the majority element. Runtime complexity = O(n).
public class Solution {
public int majorityElement(int[] nums) {
int length = nums.length;
if (length == 1)
return nums[0];
int maj = nums[0];
int count = 0;
for (int i = 0; i < length; i++) {
if (count == 0) {
maj = nums[i];
count = 1;
} else if (nums[i] == maj) {
count++;
} else {
count--;
}
}
return maj;
}
}
Solution 4 Bit Manipulation
We would need 32 iterations, each calculating the number of 1's for the ith bit of all n numbers. Since a majority must exist, therefore, either count of 1's > count of 0's or vice versa (but can never be equal). The majority number’s ith bit must be the one bit that has the greater count.
Time complexity: 32 * n = T(n)
public class Solution {
public int majorityElement(int[] nums) {
int length = nums.length;
if (length == 1)
return nums[0];
int[] dig = new int[32];
for (int i = 0; i < length; i++) {
int tmp = nums[i];
for (int j = 0; j < 32; j++) {
dig[j] += tmp & 1;
tmp = tmp >> 1;
}
}
int max = 0;
int tmp = 1;
for (int i = 0; i < 32; i++) {
if (dig[i] > length / 2) {
max = max | tmp;
}
tmp = tmp << 1;
}
return max;
}
}
Majority Element 解答的更多相关文章
- Majority Element II 解答
Question Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Th ...
- [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 ...
随机推荐
- hdu3415:最大k子段和,单调队列
题目大意:给定长度为n的数组,求出最大的区间和,其中区间长度在[1,k]之间 分析: 学动态规划的时候我们会遇到一个经典问题 最大子段和,这个题跟最大子段和很类似 不同的是区间的长度有限制,无法用原算 ...
- vim编辑器的设置文件
vim配置特点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2.自动插入文件头 ,新建C.C++源文件时自动插入表头:包括文件名 ...
- java_重写与重载的区别
重写与重载的区别 重载(Overloading)和重写(Overriding)是Java中两个比较重要的概念.但是对于新手来说也比较容易混淆.本文通过两个简单的例子说明了他们之间的区别. 定义 重载 ...
- java获取项目地址或tomcat绝对地址
在java项目中获取文件的路径,不管是相对路径还是绝对路径,其本质都是通过绝对路径去寻找. 获取项目地址 request.getSession().getServletContext().getRea ...
- 搭建Android环境
1.相关文件下载: 1.1.Java jdk下载: JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jre7-downl ...
- MapReduce新版客户端API源码分析
使用MapReduce新版客户端API提交MapReduce Job需要使用 org.apache.hadoop.mapreduce.Job 类.JavaDoc给出以下使用范例. // Create ...
- OpenMp 基本
OpenMp是由OpenMP Architecture Review Board牵头提出的,并已被广泛接受的,用于共享内存并行系统的多线程程序设计的一套指导性的编译处理方案(Compiler Di ...
- int.Tryparse() 、int.parse()、Convert.To32() 的区别
int.Tryparse() Int32.TryParse(source, result)则无论如何都不抛出异常,只会返回true或false来说明解析是否成功,如果解析失败,调用方将会得到0值. ...
- 深入javascript——构造函数和原型对象
常用的几种对象创建模式 使用new关键字创建 最基础的对象创建方式,无非就是和其他多数语言一样说的一样:没对象,你new一个呀! var gf = new Object(); gf.name = &q ...
- mysql-cluster集群原理介绍和搭建步骤(四个data/sql节点) (转)
MySQL簇概述 MySQL簇是一种技术,该技术允许在无共享的系统中部署“内存中”数据库的簇.通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求.此外,由于每个组件有自己的内存和磁盘, ...