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 ...
随机推荐
- java理论学时第七节。课后作业。
对AboutException.java的理解.在try中如果发出某类系统识别的错误,会以throw的形式抛出,在catch中可以将其截获,不显示在前端,可以选择执行别的代码. ArrayIndexO ...
- (最长上升子序列 并记录过程)FatMouse's Speed -- hdu -- 1160
http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/Other ...
- Alpha冲刺 - (10/10)
Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 本人负责的模块(共享编辑)的前端代码 ...
- C++ 中的continue理解
continue的在循环中的作用: 1. 跳过当前循环,但是还需要执行自增条件, 如下程序:当i == 3时,执行i++, 即if判定{}执行完毕,则i==4, 然后 for最后一条语句i++, 然后 ...
- C# webservice服务跟踪调试方法(转)
1.新建网站,添加服务,并创建服务. 2.打开internet 信息服务管理器,添加网站,映射到创建的服务所在网站的目录. 3.打开服务所在网站的解决方案,进行配置. 1) 设置启动选项 选择启动操作 ...
- ASP.NET微信公众号用于给指定OpenId用户发送红包
ASP.NET微信公众号用于给指定OpenId用户发送红包 微信公众号要实现对指定用户发送红包,必须指定一个存放兵发放金额的商户号,在微信商户平台里面申请商户号并获取相关参数例如发送红包所要用到的安全 ...
- WPF学习笔记(2):准确定位弹出窗
效果图:使弹出的列表框紧随在单元格的下边缘. 第一次,尝试在XAML中设置Popup的定位方式:Placement="Mouse".基本能够定位,但当在输入前移动鼠标,列表框就会随 ...
- 清理MVC4 Internaet 项目模板清理
新建项目时选择空的MVC项目 是没有Bundle 引用的非常痛苦,但是如果选择Internet模板 MVC4的模板会帮你添加一堆的JQuery 引用 打开NuGet Console 执行以下指令能帮 ...
- C++调用API获取当前时间
#include <string> #include<iostream> #include<windows.h> #include <sstream> ...
- Linux学习笔记-基本操作5
1. Linux文件操作相关函数 stat函数 :获取文件属性(从inode上获取) 返回值: 成功:0 ...