题目描述

给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。

示例 1:

输入: [3,2,3]
输出: 3

示例 2:

输入: [2,2,1,1,1,2,2]
输出: 2

思路

思路一:

利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字。

思路二:

因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数。即nums[n/2]为众数。但是在计算比较大的数组时,时间会超过限制。

思路三:

分治法,将整个数组分成两个部分,先分别筛选出这两部分中出现次数最多的数,记为left和right,如果left等于right,则返回left,如果left不等于right,则left和right都是最终结果的候选,此时需要遍历整个数组考察left和right出现的次数,出现次数较多的就是最终返回的结果。

思路四:

摩尔投票算法,先将第一个数字假设为众数,然后把计数器设为1,比较下一个数和此数是否相等,若相等则计数器加一,反之减一。然后看此时计数器的值,若为零,则将当前值设为候选众数。以此类推直到遍历完整个数组,当前候选众数即为该数组的众数。

代码实现

package Array;

import java.util.HashMap;

/**
* 169. Majority Element(求众数)
* 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
* 你可以假设数组是非空的,并且给定的数组总是存在众数。
*/
public class Solution169 {
public static void main(String[] args) {
Solution169 solution169 = new Solution169();
int[] nums = new int[]{3, 2, 3};
System.out.println(solution169.majorityElement_4(nums)); } /**
* 利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字。
*
* @param nums
* @return
*/
public int majorityElement(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num :
nums) {
Integer cnt = map.get(num);
if (cnt == null) {
cnt = 1;
} else {
cnt++;
}
if (cnt > nums.length / 2) {
return num;
}
map.put(num, cnt);
}
return 0;
} /**
* 因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数。即nums[n/2]为众数。但是在计算比较大的数组时,时间会超过限制。
*
* @param nums
* @return
*/
public int majorityElement_2(int[] nums) {
int n = nums.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
return nums[n / 2];
} /**
* 分治法,将整个数组分成两个部分,先分别筛选出这两部分中出现次数最多的数,记为left和right,如果left等于right,则返回left
* 如果left不等于right,则left和right都是最终结果的候选,此时需要遍历整个数组考察left和right出现的次数,出现次数较多的就是最终返回的结果。
*
* @param nums
* @return
*/
public int majorityElement_3(int[] nums) {
return find(nums, 0, nums.length - 1);
} public int find(int[] nums, int begin, int end) {
if (begin == end) {
return nums[begin];
} else {
int mid = (begin + end) / 2;
int left = find(nums, begin, mid);
int right = find(nums, mid + 1, end);
//左右两部分的众数相同 则这个数是这部分的众数
if (left == right) {
return left;
} else {
//左右两部分的众数不相同 则这两个数都有可能是这部分的众数,那么遍历这个数组,看一下哪个数字的出现次数多
int countLeft = 0;
int countRight = 0;
for (int i = begin; i <= end; i++) {
if (nums[i] == left) {
countLeft++;
} else if (nums[i] == right) {
countRight++;
}
}
if (countLeft > countRight) {
return left;
} else {
return right;
}
}
}
} /**
* 摩尔投票算法,先将第一个数字假设为众数,然后把计数器设为1,比较下一个数和此数是否相等,若相等则计数器加一,反之减一。
* 然后看此时计数器的值,若为零,则将当前值设为候选众数。以此类推直到遍历完整个数组,当前候选众数即为该数组的众数。
*
* @param nums
* @return
*/
public int majorityElement_4(int[] nums) {
int maj = nums[0];
int count = 1;
for (int num : nums) {
if (maj == num) {
count++;
} else {
count--;
if (count == 0) {
maj = num;
count = 1;
}
}
}
return maj;
} }

Leetcode#169. Majority Element(求众数)的更多相关文章

  1. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. 169. Majority Element求众数

    网址:https://leetcode.com/problems/majority-element/ 参考:https://blog.csdn.net/u014248127/article/detai ...

  4. 169 Majority Element 求众数 数组中出现次数超过一半的数字

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素.你可以假设数组是非空的,并且数组中的众数永远存在. 详见:https://leetcode.com/p ...

  5. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  6. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  7. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

随机推荐

  1. bzoj1791[IOI2008]Island岛屿(基环树+DP)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1791 题目大意:给你一棵n条边的基环树森林,要你求出所有基环树/树的直径之和.n< ...

  2. Day035--Python--管道, Manager, 进程池, 线程切换

    管道 #创建管道的类: Pipe([duplex]):在进程之间创建一条管道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象,强调一点:必须在产生Process ...

  3. Haproxy 安装初体验

    20180916 haproxy Haproxy简介 Haproxy是一款免费的.快速的和稳定的解决方案,提供HA和LB功能,同时对基于TCP的应用和HTTP的应用进行代理,对于流量很大的web站点来 ...

  4. (计算几何基础 叉积) nyoj68-三点顺序

    68-三点顺序 内存限制:64MB 时间限制:1000ms 特判: No通过数:27 提交数:43 难度:3 题目描述: 现在给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,现在让你判断 ...

  5. https服务器配置部署

    现在很多网站都要是https,包括我之前做的微信小程序都是需要使用https传输的,特将之前学习的记录下,以防忘记 一. 1.HTTPS简介 HTTPS其实是有两部分组成:HTTP + SSL / T ...

  6. Normal Equation

    一.Normal Equation 我们知道梯度下降在求解最优参数\(\theta\)过程中需要合适的\(\alpha\),并且需要进行多次迭代,那么有没有经过简单的数学计算就得到参数\(\theta ...

  7. C++ 文件保存

      文件保存: #include <sstream>#include <fstream>using namespace std; /*save to path ./record ...

  8. nginx的rewrite指令

    需求:我们在接收到一个/api/upload 开头的url的时候 要转发到另外一个地址.但是又不想追加/api/upload  只想追加/upload 地址. Nginx提供了rewrite指令,用于 ...

  9. POJ - 2528 Mayor's posters (离散化+线段树区间修改)

    https://cn.vjudge.net/problem/POJ-2528 题意 给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张? 分析 ...

  10. PHP7 网络编程(五)进程间通信【待】

    https://blog.csdn.net/godleading/article/details/78391159