持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第29天,点击查看活动详情
Leetcode : leetcode-cn.com/problems/zi…
GitHub : github.com/nateshao/le…
剑指 Offer 39. 数组中出现次数超过一半的数字
题目描述 :数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
难度:简单
示例:
输入: [1, 2, 3, 2, 2, 2, 5, 4, 2] 输出: 2
java 傻瓜式
/**
* 需要的数字出现次数多于一半 那么排序后必定在中间
*
* @param nums
* @return
*/
public static int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
}
Go
package main
import "sort"
func main() {
}
func majorityElement(nums []int) int {
num := sort.IntSlice(nums)
sort.Sort(num)
return num[len(num)/2]
}
解题思路:
本文将 “数组中出现次数超过一半的数字” 简称为 “众数” 。
需要注意的是,数学中众数的定义为 “数组中出现次数最多的数字” ,与本文定义不同。
本题常见的三种解法:
- 哈希表统计法:遍历数组 nums ,用HashMap统计各数字的数量,即可找出众数。此方法时间和空间复杂度均为O(N)。
- 数组排序法:将数组nums 排序,数组中点的元素一定为众数。
- 摩尔投票法:核心理念为票数正负抵消。此方法时间和空间复杂度分别为O(N)和0(1),为本题的 最佳解法。
摩尔投票法:
设输入数组 nums 的众数为x,数组长度为n。
推论一: 若记众数的票数为+1,非众数的票数为-1,则一定有所有数字的票数和> 0。 推论二: 若数组的前a个数字的票数和=0,则数组剩余(n-a)个数字的票数和一定仍> 0,即后(n- a)个数字的众数仍为x。
根据以上推论,记数组首个元素为n1,众数为x,遍历并统计票数。当发生票数和= 0时,剩余数组的众 数-定不变,这是由于:
- 当n1=x:抵消的所有数字中,有一半是众数x。
- 当n1≠x:抵消的所有数字中,众数x的数量最少为0个,最多为-半。
利用此特性,每轮假设发生票数和 = 0 都可以 缩小剩余数组区间 。当遍历完成时,最后一轮假设的数字即为众数。
算法流程:
-
初始化: 票数统计
votes = 0, 众数x; -
循环: 遍历数组
nums中的每个数字num;- 当 票数
votes等于 0 ,则假设当前数字num是众数; - 当
num = x时,票数votes自增 1 ;当num != x时,票数votes自减 1 ;
- 当 票数
-
返回值: 返回
x即可;
复杂度分析:
-
时间复杂度 O(N) : N 为数组
nums长度。 -
空间复杂度 O(1) :
votes变量使用常数大小的额外空间。
public static int majorityElement1(int[] nums) {
int x = 0, votes = 0;
for(int num : nums){
if(votes == 0) x = num;
votes += num == x ? 1 : -1;// votes = votes + ( num == x ? 1 : -1);
}
return x;
}
拓展: 由于题目说明 给定的数组总是存在多数元素 ,因此本题不用考虑 数组不存在众数 的情况。若考虑,需要加入一个 “验证环节” ,遍历数组 nums 统计 x 的数量。
- 若
x的数量超过数组长度一半,则返回x; - 否则,返回未找到众数;
时间和空间复杂度不变,仍为 O(N)和 O(1) 。
public int majorityElement11(int[] nums) {
int x = 0, votes = 0, count = 0;
for(int num : nums){
if(votes == 0) x = num;
votes += num == x ? 1 : -1;
}
// 验证 x 是否为众数
for(int num : nums)
if(num == x) count++;
return count > nums.length / 2 ? x : 0; // 当无众数时返回 0
}
代码
package com.nateshao.sword_offer.topic_31_majorityElement;
import java.util.Arrays;
/**
* @date Created by 邵桐杰 on 2021/12/5 17:16
* @微信公众号 千羽的编程时光
* @个人网站 www.nateshao.cn
* @博客 https://nateshao.gitee.io
* @GitHub https://github.com/nateshao
* @Gitee https://gitee.com/nateshao
* Description: 剑指 Offer 39. 数组中出现次数超过一半的数字
* 题目描述:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数
* 字。如果不存在则输出 0。
*/
public class Solution {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 2, 2, 5, 4, 2};
int i1 = majorityElement1(arr);
int i2 = majorityElement2(arr);
int i3 = majorityElement3(arr);
System.out.println("i = " + i1); // i = 2
System.out.println("i2 = " + i2);
System.out.println("i3 = " + i3);
}
/******************** 精选 *********************/
public static int majorityElement1(int[] nums) {
int x = 0, votes = 0;
for(int num : nums){
if(votes == 0) x = num;
votes += num == x ? 1 : -1;// votes = votes + ( num == x ? 1 : -1);
}
return x;
}
/**************** 拓展 *********************/
public int majorityElement11(int[] nums) {
int x = 0, votes = 0, count = 0;
for(int num : nums){
if(votes == 0) x = num;
votes += num == x ? 1 : -1;
}
// 验证 x 是否为众数
for(int num : nums)
if(num == x) count++;
return count > nums.length / 2 ? x : 0; // 当无众数时返回 0
}
/****************** 剑指offer **********************/
/**
* 思路:将首次出现的数 count+1,与之后的数进行比较,相等则+1,否则—1,
* 最后进行校验是否超过长度的一半。
*
* @param nums
* @return
*/
public static int majorityElement2(int[] nums) {
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) candidate = num;
count += (num == candidate) ? 1 : -1;
}
return checkMoreThanHalf(nums, candidate) ? candidate : 0;
}
private static boolean checkMoreThanHalf(int[] array, int number) {
int times = 0;
for (int i : array) {
if (i == number) times++;
}
return times * 2 >= array.length;
}
public static int majorityElement3(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}