题目描述:

给定一个大小为 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ 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. editormd 富文本编辑器转 html

    // html <div id="markdown-view"> <textarea id="markdownView" style=&quo ...

  2. vue项目在IE浏览器和360兼容模式下页面不显示问题,亲测有效

    解决方法:安装 "babel-polyfill" 1.命令:cnpm install --save-dev babel-polyfill 2.在入口main.js文件引入:impo ...

  3. go语言入门(6)复合类型

    1,分类 2,指针 指针是一个代表着某个内存地址的值.这个内存地址往往是在内存中存储的另一个变量的值的起始位置. 1)基本操作 Go语言虽然保留了指针,但与其它编程语言不同的是: 默认值 nil,没有 ...

  4. yum list报一些error的组件

    1 删除那些无效的参数配置,就不再报错了

  5. (二十四)Ubuntu16.04配置ADB调试环境

    一.安装adb 1.可以通过 apt-get install android-tools-adb 来安装adb sudo add-apt-repository ppa:nilarimogard/web ...

  6. 01_日志采集框架Flume简介及其运行机制

    离线辅助系统概览: 1.概述: 在一个完整的大数据处理系统中,除了hdfs+mapreduce+hive组成分析系统的核心之外,还需要数据采集.结果数据导出. 任务调度等不可或缺的辅助系统,而这些辅助 ...

  7. Java NIO 学习笔记一

    缓冲区操作 进程执行I/O操作,归结起来就是向操作系统发出请求,它要么把缓存区例的数据排干(写),要么用数据把数据区填满(读).进程使用这一机制处理所有数据进出操作. 进程使用read()系统调用,要 ...

  8. Python&Selenium 关键字驱动测试框架之数据文件解析

    摘要:在关键字驱动测试框架中,除了PO模式以及一些常规Action的封装外,一个很重要的内容就是读写EXCEL,在团队中如何让不会写代码的人也可以进行自动化测试? 我们可以将自动化测试用例按一定的规格 ...

  9. TypeHandler简介及配置(mybatis源码篇)

    作者:南柯梦 Mybatis中的TypeHandler是什么? 无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时,都会用类型处理器 ...

  10. Bootstrap-轮播图-No.4

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