首页 技术 正文
技术 2022年11月15日
0 收藏 484 点赞 3,842 浏览 1780 个字

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:

  1. If the counter is 0, we set the current candidate to x and the counter to 1.
  2. 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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905