题目描述:

给定一个大小为 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ 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. intellij IDE 破解 简单 License server 法

    http://idea.iteblog.com/key.php

  2. luogu4302字符串折叠题解--区间DP

    题目链接 https://www.luogu.org/problemnew/show/P4302 分析 很明显一道区间DP题,对于区间\([l,r]\)的字符串,如果它的字串是最优折叠的,那么它的最优 ...

  3. 轮播图--使用原生js的轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 函数——箭头函数&自执行函数(二)

    一.箭头函数是在es6中添加的一种规范,它相当于匿名函数,简化了函数的定义. 1.语法 a.function用var,let,cost来表示: b.参数要写在第一个等号后面:   参数有多个,需要加一 ...

  5. etcd简单测试类java版

    为了方便现场安装完了etcd集群后确认集群是否好用,简单写了个测试类,网上搜的有点乱还有些不能运行,在这里再整理一个能够直接运行的 1.我把etcd的API设成3版本了,调用使用的jetcd,功能挺多 ...

  6. java 项目坑记录

    1. spring项目引入了lombok jar包,且已在类上面添加@Data注释,还是关联不到 get() 和 set() 方法 需要安装扩展包 如果在线安装失败,提示错误readtime out, ...

  7. String、StringBuffer和StringBuilder区别

    String.StringBuffer和StringBuilder区别 1.长度是否可变 String 是被 final 修饰的,他的长度是不可变的,就算调用 String 的concat 方法,那也 ...

  8. 03 js事件循环

    1. js里重要的是事件循环. 参考:https://nodejs.org/en/docs/guides/ 中文版:https://github.com/nodejs/nodejs.org/tree/ ...

  9. StringBuffer常用方法

    StringBuffer常用的方法 package com.mangosoft.java.string; /** * 字符串特点:字符串是常量,它们的值在创建之后不能更改. * * 字符串的内容一旦发 ...

  10. Python正则及geometer正则截图讲解

    正则表达式   语法: 1 2 3 4 5 6 import re #导入模块名   p = re.compile("^[0-9]")  #生成要匹配的正则对象 , ^代表从开头匹 ...