题目描述:

给定一个大小为 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ 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. css的一些基础方法

    1.css样式表分别有: 内联样式表 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  2. JavaScript笔记(5)

    1.DOM操作 常用的DOM操作 document.getElementById(id); //返回指定id的元素,通用 document.getElementsByTagName(tagName); ...

  3. Webpack4 splitChunks配置,代码分离逻辑

    博客不知道啥时候写的了,一直在草稿箱没写完,突然感觉今年过去大半了,又没怎么写博客.写写完,有始有终 1.代码分离升级 原来项目代码分离是通过下面的配置,基于bundle-loader插件,通过rou ...

  4. 使用openSSL构造一个支持https的nodejs服务器

    首先通过下面的链接下载openSSL https://slproweb.com/products/Win32OpenSSL.html 下载完毕后,执行openssl进入交互式界面: 使用命令生成pri ...

  5. Computer Vision_1_Active Appearance Models :Active Appearance Models——1998

    此为计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面. 1. Active Appearance Models 活动表观模型和活动轮廓模型基本思想来源 Snake,现在 ...

  6. web应用原理之——会话

    会话是大家开发Java EE Web应用的常用技术,那么会话是什么,会话的用途还有工作原理又是什么,下面就简单说一说. 什么是会话,在web应用中,作为客户端的浏览器,通过请求/响应这种模式访问同一个 ...

  7. 03.Zabbix应用服务监控

    一.Zabbix监控Nginx 1.1 实验环境 服务器系统 角色 IP CentOS 7.4 x86_64 Zabbix-Server 192.168.90.10 CentOS 7.4 x86_64 ...

  8. Android使用WebView打包网页成app

    原生app的开发成本和网页相比相对较高,所以越来越多的app使用网页来作为界面,甚至完全将一个网站封装成app,可以提高开发速度,还能基本实现跨平台. 下面以Android为例,在ubuntu-14. ...

  9. SpiderMan成长记(爬虫之路)

    第一章 爬虫基础 1.1 爬虫基本原理 1.2 请求库 -- urllib库的使用 1.3 请求库 -- requests库的使用 1.4 数据解析 -- 正则基础 1.5 数据解析 -- lxml与 ...

  10. FirstWriting

    在很久很久以前,你拥有我我拥有你 <外面的世界> 在很久很久以前我就有搞一个类似博客的东西的想法,不过一直都没有尝试着搞-- 某天(10号左右吧)刷知乎看到github和hexo可以搭建博 ...