Leetcode题目169.求众数(简单)
题目描述:
给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ 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.求众数(简单)的更多相关文章
- 【Leetcode】【简单】【169求众数】【JavaScript】
题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...
- Leetcode之分治法专题-169. 求众数(Majority Element)
Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode(169. 求众数)
问题描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- 169.求众数 leetcode Javascript
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...
- Java实现 Leetcode 169 求众数
public static int majorityElement(int[] nums) { int num = nums[0], count = 1; for(int i=1;i<nums. ...
- Leecode刷题之旅-C语言/python-169求众数
/* * @lc app=leetcode.cn id=169 lang=c * * [169] 求众数 * * https://leetcode-cn.com/problems/majority-e ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- 力扣题目汇总(丑数,重复N的元素,求众数)
丑数 1.题目描述 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 ...
随机推荐
- USB相关资料汇总
[1]USB规范,一切的一切,基本的基本,天书级别USB_11_spec(中文).pdf USB1.1规范(中文版) usb_20.pdf USB2.0规 ...
- 如何使用classnames模块库为react动态添加class类样式
摘要 在react中添加动态的css时,传统的方式较为繁琐,今天刚好学习到一个模块库可以便捷的解决这个问题.对的,它就是“classnames”. classnames模块库 npm安装 npm in ...
- JavaScript随机验证码
利用canvas制作一个随机验证码: 1.clearRect:context.clearRect(x,y,width,height);清空给定矩形内的指定像素 2.fillStyle:设置画笔的颜色 ...
- elment-ui表单验证
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-widt ...
- Webstorm2017.3.3软件的安装使用
下载 ▶进入jetbrains的官方网站点击download,即下载开始.官方网站链接:http://www.jetbrains.com/webstorm/ 安装 ▶双击刚下载完成的.exe文件开始进 ...
- 【jekins】安装jdk遇到的坑
首先我电脑版本为win10 64 在安装JDK时先安装了jdk包,安装路径为D:\Java\jdk1.8.0_171,装完jdk后,自动安装jre,我将其安装在D:\Java\jre1.8.0_171 ...
- linux入门常用指令4.挂载数据盘
挂载硬盘 #查看当前分区情况 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 5G 0 dis ...
- web开发:javascript高级
一.事件案例 二.循环绑定之变量污染 三.事件的绑定与取消 四.事件对象 一.事件案例 <!DOCTYPE html> <html> <head> <meta ...
- 【Java基础 项目实例--Bank项目5】Account 和 customer 对象等 继承、多态、方法的重写
延续 Java基础 项目实例--Bank项目4 实验要求 实验题目 5: 在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount 实验目的: 继承 ...
- 用tensorflow实现SVM
环境配置 win10 Python 3.6 tensorflow1.15 scipy matplotlib (运行时可能会遇到module tkinter的问题) sklearn 一个基于Python ...