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 ...
随机推荐
- [HEOI 2013 day2] 钙铁锌硒维生素 (线性代数,二分图匹配)
题目大意 给定两个n阶方阵,方阵B的行i能匹配方阵A的行j当且仅当在第一个方阵中用行向量i替换行向量j后,第一个方阵满秩,显然这是个二分图匹配问题,问是否存在完美匹配,如果存在,还要输出字典序最小的方 ...
- python calendar标准库基础学习
# -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#标准库:日期时间基础学习:calendar:处理日期#例1import calen ...
- [Redux] Passing the Store Down Explicitly via Props
n the previous lessons, we used this tool to up level variable to refer to the Redux chore. The comp ...
- 获取sqlserver数据库中所有库、表、字段名的方法
获取sqlserver数据库中所有库.表.字段名的方法 2009年03月12日 星期四 下午 12:51 1.获取所有数据库名: SELECT Name FROM Master..SysDatabas ...
- html与css的移动端与pc端需要注意的事项
一个移动端与pc端之间最主要的也就是尺寸问题,苹果与安卓的机型尺寸大小相差甚多,一个尺寸都会影响用户的体验.那么我们来了解一下一些常用的解决方法. 一般在网页中都会在头部有一些这样的代码 <me ...
- mysql的锁--行锁,表锁,乐观锁,悲观锁
一 引言--为什么mysql提供了锁 最近看到了mysql有行锁和表锁两个概念,越想越疑惑.为什么mysql要提供锁机制,而且这种机制不是一个摆设,还有很多人在用.在现代数据库里几乎有事务机制,aci ...
- oracle启动,提示“LRM-00109: could not open parameter file”
转载自 http://blog.sina.com.cn/s/blog_53e731b70101liku.html oracle启动,提示“LRM-00109: could not open par ...
- IIS负载均衡相关
1. IIS负载均衡 (比较简单的例子,能看到效果) 2.nginx+iis实现负载均衡 3.Windows平台分布式架构实践 - 负载均衡 4.Net分布式系统:Keepalived+LVS+Ngi ...
- STL set容器添加结构体并排序
#include <iostream> #include <string> #include <cstring> //strcpy #include <cst ...
- SATA接口硬盘加密器
加密卡置于主板与硬盘.光驱之间,透明实时地对写入数据进行加密,对读出数据进行解密,有效防止信息被窃.未经授权的阅读和修改,以及硬盘.光盘丢失.被盗.废弃.非法用户访问而引发的敏感信息泄密问题,为用户打 ...