LeetCode 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.
也就是找数组中出现次数大于一半的数字,题目保证这个数字存在。
方法一:位操作
针对数组中每一个数的每一位,计算每一位上0和1出现的次数,取出现多的作为最终数字的当前位。
代码如下:时间复杂度32n=O(n),空间复杂度O(1)
// bit操作
public int majorityElement(int[] nums) {
int temp = 0, ans = 0, count0 = 0, count1 = 0;
for (int i = 0; i < 32; i++) {
count0 = 0;
count1 = 0;
for (int j = 0; j < nums.length; j++) {
if (((nums[j] >>> i) & 1) == 1)
count1++;
else
count0++;
}
if (count1 > count0)
temp += 1;
if (i < 31)
temp >>>= 1;
}
for (int i = 0; i < 32; i++) {
ans += ((temp >> i) & 1);
if (i < 31)
ans <<= 1;
}
return temp;
}
方法二:HashMap,
空间复杂度O(n),时间复杂度O(n),相对位操作更耗时。
// 使用hashMap
public int majorityElement(int[] nums) {
int temp = 0, ans = 0, count0 = 0, count1 = 0;
Map<Integer, Integer> countMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
countMap.put(nums[i], countMap.getOrDefault(nums[i], 0) + 1);
if (countMap.get(nums[i]) > nums.length / 2)
return nums[i];
}
return 0;
}
方法三:计数法,
这个方法应该是最优解法吧。相比位操作更好。
该方法的思路是从局部思考问题,前2K个数字中,某个数无法做成majority element,但它也有可能出现很多次,但是最终在某个点上会被其他不相同的数字中和了。从后面再计数。最终找到的就是majority element。(描述的不好,直接看代码理解吧)。
假设被中和的是majority element,不用担心,因为你干掉了和你一样多的对手,在后续的子数组中,你还是大头。
假设被中和的不是,那么后续子数组中,你还是不能。
代码如下:
public int majorityElement(int[] nums) {
int count = 0;
int ans = nums[0];
for (int i : nums) {
if (count == 0)
ans = nums[i];
if (ans == nums[i])
count++;
else
count--;
}
return ans;
}
LeetCode 169. Majority Element解题方法的更多相关文章
- LeetCode 169 Majority Element 解题报告
题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- [LeetCode] 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 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java [Leetcode 169]Majority Element
题目描述: Given an array of size n, find the majority element. The majority element is the element that ...
随机推荐
- ajax请求也可以用form表单向后台提交数据!!!!
激动的我简直语无伦次,不说了上代码,用ajax实现form表单数据的请求,啦啦啦啦 html: <form> <input name="userName" val ...
- noip第16课作业
1. 猴子吃桃 [问题描述] 猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个:第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个:以后每天都吃前一天剩下的一半多一个,到第10天想再吃时 ...
- TypeScript开发环境搭建(Visual studio code)
使用Visual Studio Code搭建TypeScript开发环境 1.TypeScript是干什么的 ? TypeScript是由微软Anders Hejlsberg(安德斯·海尔斯伯格,也是 ...
- Hadoop读写流程
写流程 读流程 HDFS写数据流程 HDFS读数据流程 网络拓扑-节点距离计算 节点距离:两个节点到达最近的共同祖先的距离总和
- vnc搭建
PS:偶以前基本不用Linux的远程图形桌面,前几天有开发的同事配置CentOS的vnc有问题,找我解决,就顺便记录总结一下,这个总结是比较完整的.下面的配置在CentOS5.x和6.x.SUSE企业 ...
- matlab 中fft的用法
一.调用方法X=FFT(x):X=FFT(x,N):x=IFFT(X);x=IFFT(X,N) 用MATLAB进行谱分析时注意: (1)函数FFT返回值的数据结构具有对称性. 例:N=8;n=0:N- ...
- Python基础语法-内置数据结构之列表
列表的一些特点: 列表是最常用的线性数据结构 list是一系列元素的有序组合 list是可变的 列表的操作, 增:append.extend.insert 删:clear.pop.remove 改:r ...
- 关于c#的知识博客
C#.NET Request相关操作:获取IP.端口其他一些: http://www.cnblogs.com/moss_tan_jun/archive/2011/02/10/1950575.html
- openvSwitch tunnel
ovs tunnel 实验拓扑 实验拓扑搭建基础脚本 #vbox虚拟机1 ip netns add left ip link add name veth1 type veth peer name sw ...
- 【译】准备好你求职时候用的 GitHub 账号
我目前正在招聘,很多人分享了他们的GitHubs个人资料和项目,但是维护得很差,所以我决定为活跃的求职者写一个小指南. 无论是否合理,技术招聘人员倾向于从您的GitHub个人资料中推断出很多关于您的信 ...