题目描述:

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

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

示例 1:

输入: [3,2,3]
输出: 3
示例 2: 输入: [2,2,1,1,1,2,2]
输出: 2

思路分析:

思路一:暴力枚举

class Solution {
public static int majorityElement(int[] nums) { if (nums == null || nums.length == 0) {
return Integer.MIN_VALUE;
}
for (int i = 0; i < nums.length; i++) {
int res = nums[i];
int count = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] == res) {
count++;
}
}
if (count > nums.length / 2) {
return res;
}
}
return Integer.MIN_VALUE;
}
}

时间复杂度:O(n^2)

空间复杂度:O(1)

思路二:Hash,用空间换时间

class Solution {
//用hash,空间换时间,时间复杂度O(n),空间复杂度
public static int majorityElement(int[] nums) { if (nums == null || nums.length == 0) {
return Integer.MIN_VALUE;
}
Map<Integer, Integer> map = new HashMap<>(nums.length);
for (int i = 0; i < nums.length; i++) {
Integer count = map.get(nums[i]);
if (count != null) {
count++;
map.put(nums[i], count);
} else {
map.put(nums[i], 1);
}
}
for (Integer key : map.keySet()) {
if (map.get(key) > nums.length / 2) {
return key;
}
}
return Integer.MIN_VALUE;
} }

时间复杂度:O(n)

空间复杂度:O(n)

思路三:排序法:因为是众数,所以排序过后,数组中间的数就是众数

class Solution {

    public static int majorityElement(int[] nums) {

        if (nums == null || nums.length == 0) {
return Integer.MIN_VALUE;
}
Arrays.sort(nums);
return nums[nums.length / 2]; }
}

时间复杂度:O(NlogN)

空间复杂度:O(1)

思路四:摩尔投票法

其核心思想就是:抵消

最差的情况就是其他所有的数都跟众数做了抵消,但是由于众数出现的次数大于1/2,所以最终剩下的还是众数。

class Solution {
//摩尔投票算法
public static int majorityElement(int[] nums) { if (nums == null || nums.length == 0) {
return Integer.MIN_VALUE;
} int target = nums[0];
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
count++;
} else {
if (count >= 1) {
count -= 1;
} else {
target = nums[i];
}
}
}
return target;
}
}

时间复杂度:O(N)

空间复杂度:O(1)

Leetcode题目169.求众数(简单)的更多相关文章

  1. 【Leetcode】【简单】【169求众数】【JavaScript】

    题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...

  2. Leetcode之分治法专题-169. 求众数(Majority Element)

    Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...

  3. [LeetCode] Majority Element 求众数

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

  4. LeetCode(169. 求众数)

    问题描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  5. 169.求众数 leetcode Javascript

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...

  6. Java实现 Leetcode 169 求众数

    public static int majorityElement(int[] nums) { int num = nums[0], count = 1; for(int i=1;i<nums. ...

  7. Leecode刷题之旅-C语言/python-169求众数

    /* * @lc app=leetcode.cn id=169 lang=c * * [169] 求众数 * * https://leetcode-cn.com/problems/majority-e ...

  8. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  9. 力扣题目汇总(丑数,重复N的元素,求众数)

    丑数 1.题目描述 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 ...

随机推荐

  1. C# 连接 Socks5 代理

    public class Socks5ProxyHelp { private Socks5ProxyHelp() { } public static string[] errorMsgs = { &q ...

  2. Git FLS的使用

    克隆git地址后,一些文件内容被隐藏. 显示如下: version https://git-lfs.github.com/spec/v1oid sha256:xxxxxxxxxxxxxxxxxxxxx ...

  3. JS基础_实参可以是任何值

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. jquery.serializejson.min.js的妙用

    关于这个jquery.serializejson.min.js插件来看,他是转json的一个非常简单好用的插件. 前端在处理含有大量数据提交的表单时,除了使用Form直接提交刷新页面之外,经常碰到的需 ...

  5. js之数据类型(对象类型——单体内置对象——JSON)

    JSON(Java Script Object Notation)使用JavaScript语法,是用于存储和传输数据的格式,通常用于服务端向网页传递数据.JSON格式仅仅是一个文本,文本可以被任何编程 ...

  6. React的性能优化

    1. 在constructor中绑定事件函数的this指向 把一个函数赋值给一个变量,然后用那个变量去执行函数会造成this的丢失,所以需要绑定this,把绑定放在构造函数中可以保证只绑定一次函数,如 ...

  7. sql临时表 通过临时表循环处理数据

    -- 创建临时表 IF OBJECT_ID('tempdb.dbo.#temprecord','U') IS NOT NULL DROP TABLE dbo.#temprecord; GO SELEC ...

  8. 【loj#2133 && luoguP2178】[NOI2015]品酒大会

      题目传送门:loj#2133 luoguP2178   简要题意:给定一个字符串\(s\),每个后缀都有权值,对于每个长度\(len\),求出所有最长公共前缀\(\geq len\)的后缀对的总数 ...

  9. AJAX配置csrf

    // 从COokie取CSRF TOKEN的值 function getCookie(name) { var cookieValue = null; if (document.cookie & ...

  10. mybatis-generator-gui

    mybatis-generator-gui介绍 mybatis-generator-gui是基于mybatis generator开发一款界面工具, 本工具可以使你非常容易及快速生成Mybatis的J ...