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 ...
随机推荐
- 【repost】对JAVASCRIPT匿名函数的理解(透彻版)
Query片段: view plaincopy to clipboardprint? (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其 ...
- Beta阶段第二篇Scrum冲刺博客-Day1
1.站立式会议 提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 张晨晨:交接进组 郭琪容:明确任务并学习 吴玲:明确接 ...
- hadoop的两大核心之一:HDFS总结
什么是HDFS? hadoop distributed file system(hadoop分布式文件系统) 是一种允许文件通过网络在多台主机上分享的文件系统, 可让多机器上的多用户分享文件和存储空间 ...
- vdscode连接git服务器(以码云为例)
准备工作:先下载并安装git客户端 1.在码云或者github上新建项目,获得新建项目的地址,得到一个类似:https://gitee.com/zhangshitongsky/vueTest.git ...
- Nginx 实现端口转发
https://www.cnblogs.com/zhaoyingjie/p/7248678.html Nginx 实现端口转发 什么是端口转发 当我们在服务器上搭建一个图书以及一个电影的应用,其中图书 ...
- Android-Kotlin-when&类型推断
Kotlin的when表达式 TextEngine 描述文字处理对象: package cn.kotlin.kotlin_base02 /** * 描述文字处理对象 * * val textConte ...
- MVC5控制器传值的三种方式(ViewData,ViewBag,TempData),刚刚学习MVC5的新手,希望各位大神多多指教
mvc传值的三种方式:1.ViewData 在使用过程中需要类型转换 例子: ViewData["MyTitle"]="ViewData传值"; 引用: @Vi ...
- jquery banner 轮播配置方法
1 概述 Banner可以作为网站页面的横幅广告,也可以作为游行活动时用的旗帜,还可以是报纸杂志上的大标题.Banner主要体现中心意旨,形象鲜明表达最主要的情感思想或宣传中心.在以往很多项目中主要体 ...
- #loj3090 [BJOI2019] 勘破神机
简单线性代数练习题 首先翻开具体数学生成函数一章,可以发现\(F(n),G(n)\)满足以下递推式 \[F(n)=F(n-1)+F(n-2),F(0)=1,F(1)=1\] \[G(n)=4G(n-2 ...
- [NOIP2018]赛道修建(二分+multiset)
考场上打了一个 \(vector\) 解法,因为我当时不会 \(multiset\) 好吧,我来讲一讲今年的 \(tgD1T3\) 首先,这题 \(55\) 分是不难想的 1. \(b_i=a_i+1 ...