【四种解法】剑指 Offer 39. 数组中出现次数超过一半的数字
立志用最少的代码做最高效的表达
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。示例 1:
输入: [1, 2, 3, 2, 2, 2, 5, 4, 2]
输出: 2限制:
1 <= 数组长度 <= 50000
代码一:HashMap
做法:利用hashmap统计次数,最后输出出现次数最多的变量
时间复杂度:O(n)
空间复杂度:O(n)
显而易见的缺点:没有利用“出现次数超过一半”这个条件
static class Solution1 {public int maxCount(int[] nums) {if(nums.length == 0) return -1;Map<Integer, Integer> map = new HashMap<>();int max = 0, max_value = -0x7fffffff, len = nums.length;for(int i = 0; i < len; i++) {if(map.putIfAbsent(nums[i],1) != null) // 如果为空,则赋值为0map.put(nums[i], map.get(nums[i])+1);if(max_value < map.get(nums[i])) { max_value = map.get(nums[i]); max = nums[i]; }}return max;}}
代码二:针对代码一的优化
针对思路一的优化:因为出现次数超过一半即可,因此无需遍历完整张表,当某个值出现次数超过一半,结束循环,输出即可。
时间复杂度:O(n/2)~O(n)
空间复杂度:O(n)
class Solution {public int maxCount(int[] nums) {Map<Integer, Integer> map = new HashMap<>();int max = 0, max_value = -0x7fffffff, len = nums.length;for(int i = 0; i < len; i++) {if(map.putIfAbsent(nums[i],1) != null) // 如果为空,则赋值为0map.put(nums[i], map.get(nums[i])+1);if(map.get(nums[i]) > len/2)return nums[i];}return -1;}
}
代码三:排序算法
由于出现次数超过一半,因此排序后,数组的中间值一定为该数。
时间复杂度:O(nlogn)
空间复杂度:O(1)
class Solution3 {public int maxCount(int[] nums) {Arrays.sort(nums);return nums[nums.length/2];}
}
代码四:摩尔投票法
可以理解为同归于尽大法,一个对一个,最后活下来的一定是出现次数超过一半的
class Solution {public int majorityElement(int[] nums) {int res = 0, count = 0;for(int i : nums)if(count == 0) {res = i; count++;}elsecount += (res==i ? 1 : -1);return res;}
}
完整可运行Java代码
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;public class 剑指Offer39_数组中次数超一半的数字 {// 1、hashmap:双O(n)// 做法:利用hashmap统计次数,最后输出出现次数最多的变量// 显而易见的缺点:没有利用“出现次数超过一半”这个条件static class Solution1 {public int maxCount(int[] nums) {if(nums.length == 0) return -1;Map<Integer, Integer> map = new HashMap<>();int max = 0, max_value = -0x7fffffff, len = nums.length;for(int i = 0; i < len; i++) {if(map.putIfAbsent(nums[i],1) != null) // 如果为空,则赋值为0map.put(nums[i], map.get(nums[i])+1);if(max_value < map.get(nums[i])) { max_value = map.get(nums[i]); max = nums[i]; }}return max;}}// 2、针对思路一的优化;因为出现次数超过一半即可,因此无需遍历完整张表,当某个值出现次数超过一半,结束循环,输出即可。// 时间O(n/2),空间O(n)static class Solution2 {public int maxCount(int[] nums) {Map<Integer, Integer> map = new HashMap<>();int max = 0, max_value = -0x7fffffff, len = nums.length;for(int i = 0; i < len; i++) {if(map.putIfAbsent(nums[i],1) != null) // 如果为空,则赋值为0map.put(nums[i], map.get(nums[i])+1);if(map.get(nums[i]) > len/2)return nums[i];}return -1;}}// 3、排序:O(nlogn)、O(1)static class Solution3 {public int maxCount(int[] nums) {Arrays.sort(nums);return nums[nums.length/2];}}// 4、摩尔投票法,可以理解为同归于尽大法,一个对一个,最后活下来的一定是出现次数超过一半的static class Solution4 {public int majorityElement(int[] nums) {int res = 0, count = 0;for(int i : nums)if(count == 0) {res = i; count++;}elsecount += (res==i ? 1 : -1);return res;}}public static void main(String[] args) {}
}
【四种解法】剑指 Offer 39. 数组中出现次数超过一半的数字相关推荐
- 【LeetCode】剑指 Offer 39. 数组中出现次数超过一半的数字
[LeetCode]剑指 Offer 39. 数组中出现次数超过一半的数字 文章目录 [LeetCode]剑指 Offer 39. 数组中出现次数超过一半的数字 一.摩尔投票法 一.摩尔投票法 核心理 ...
- 剑指 Offer 39. 数组中出现次数超过一半的数字
剑指 Offer 39. 数组中出现次数超过一半的数字https://leetcode.cn/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban ...
- 【算法】剑指 Offer 39. 数组中出现次数超过一半的数字 【重刷】
1.概述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5
- 剑指OFFER之数组中出现次数超过一半的数字(九度OJ1370)
题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2 ...
- 《剑指offer》数组中出现次数超过一半的数字
题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2.如果 ...
- 【剑指offer】 数组中出现次数超过一半的数字
- 剑指offer:面试题39. 数组中出现次数超过一半的数字
题目:面试题39. 数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [1, ...
- 剑指offer面试题39. 数组中出现次数超过一半的数字(数组)(摩尔投票法)
题目描述 **数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素.** 思路 详见链接 代码 class Solution:def ...
- 【LeetCode】剑指 Offer 51. 数组中的逆序对
[LeetCode]剑指 Offer 51. 数组中的逆序对 文章目录 [LeetCode]剑指 Offer 51. 数组中的逆序对 package offer;public class Soluti ...
最新文章
- mysql搭建主从的目的_mysql搭建主从
- SPSiteDataQuery
- python模拟登陆
- 组件化与插件化的差别在哪里?附面试题答案
- MSDN 论坛好帮手3月首发
- OAuth 授权timestamp refused问题
- 玩转Excel系列-SUMIF函数实例教程
- 问题四十九:怎么用ray tracing画supertoroid(超级圆环)
- [转]Java程序员们最常犯的10个错误
- 用南边代称一个公司,汝竟然也不满?
- 软件设计---概要设计和详细设计
- ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN
- 纳韦斯托克斯方程的推导_什么是纳维-斯托克斯方程?
- 笔记本电脑计算机里面怎么管理,笔记本电脑如何分区,详细教您笔记本电脑怎么分区...
- 文献阅读(03)Computing Graph Neural Networks: A Survey from Algorithms to Accelerators
- Unity3D Dither 抖动Shader实现
- Android ExceptionThrowable 常见异常和解决方法 奔溃日志上报 monkey异常修改
- SCI和SCIE的区别和联系
- 5、Spring SPEL使用之--在XML中使用SPEL
- 红米android os耗电,体验真正流畅带来的快感,120Hz高刷新率的红米/Redmi K30测评报告...