LeetCode 第三大的数414. Third Maximum Number
题目
描述:给定数组中求第三大的数字;如果没有,返回最大的;时间复杂度O(n)
记得《剑指offer》才看到过这样的求第k大的题目。但是忘记具体怎么做了。只好先自己想了。
因为时间复杂度的限制,所以不能用排序,考虑声明3个空间,用于保存前三大的数字。
错误
三个空间初始化为N[0]
由于考虑不仔细,想着直接把三个空间初始化为N[0],然后从1开始遍历,发现可能直接输出第一个数字,尽管它不是第三大的。
所以应该初始化为Integer.MIN_VALUE
理解错题目
刚开始没有认真理解好题目,“如果没有”也包括像[1,2,1]这样的虽然个数有三个,但是并没有第三大的数字。如果按照原来的想法,则输入[1,1,2]会输出-2147483648。而此时应该输出最大值
解决:通过HashSet去重,得到“真正的个数”
粗心
输入[1,1,2]会输出1(应该是最大值2),尽管前面个数的判断改了,但是只有两个时候返回仍然是(nums[0]>nums[1]?nums[0]:nums[1]);
通过修改,将HashSet又重新保存回数组,再来比较:(newNums[0]>newNums[1]?newNums[0]:newNums[1]);
最终代码
public class Solution {
public int thirdMax(int[] nums) {
HashSet<Integer> integers=new HashSet<>();
for (Integer integer : nums) {
integers.add(integer);
}
int[] newNums=new int[integers.size()];
int i=0;
for (Integer integer : integers) {
newNums[i]=integer;
i++;
}
if(integers.size()==1)
return newNums[0];
else if (integers.size()==2) {
return (newNums[0]>newNums[1]?newNums[0]:newNums[1]);
}else {
//声明一个大小为3的数组,保留原数组的最大的三个,而且维护从小到大的排序
int first=Integer.MIN_VALUE,second=Integer.MIN_VALUE,third=Integer.MIN_VALUE;
for (i = 0; i < newNums.length; i++) {
int curInt = newNums[i];
if(first<curInt&&second>curInt){
first=curInt;
}else if (curInt>second&&curInt<third) {
first=second;
second=curInt;
}else if (curInt>third) {
first=second;
second=third;
third=curInt;
}
}
return first;
}
}
}
结果分析
最后只打败了20%+的java,估计还是前面HashSet去重可能用的时间有点多了。回头再想一个好一点的去重的办法!
LeetCode 第三大的数414. Third Maximum Number的更多相关文章
- C#LeetCode刷题之#414-第三大的数(Third Maximum Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- LeetCode 414. Third Maximum Number (第三大的数)
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- 414 Third Maximum Number 第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- LeetCode Array Easy 414. Third Maximum Number
Description Given a non-empty array of integers, return the third maximum number in this array. If i ...
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- leetcode 第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...
随机推荐
- Ubuntu16.04安装 2.8.5版本Ansible
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py && python get-pip.py pip install --upgrade ...
- Yarn 生产环境核心配置参数
目录 Yarn 生产环境核心配置参数 ResourceManager NodeManager Container Yarn 生产环境核心配置参数 ResourceManager 配置调度器 yarn. ...
- A Child's History of England.52
'Arthur,' said the King, with his wicked eyes more on the stone floor than on his nephew, 'will you ...
- IDEA2021.2安装与配置
https://blog.csdn.net/qq_37242720/article/details/119349394
- Camera、音频录制与Vitamio框架
一.Camera 1.概述 Android框架包含了各种相机哥相机功能的支持,是你可以在应用中捕获图像和视频. 在应用能使用设备上的相机之前,先想一想将来会如何使用此硬件: (1)Camera 应该 ...
- MyBatis(1):实现MyBatis程序
一,MyBatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- react中在hooks方法useEffect中加载异步数据
useEffect( ()=>{ (async function getPipeList(value:any) { let result= await GetPipeList(value); s ...
- xtra+binlog增量备份脚本
目录 一.备份原理 innobackupex原理 binlog原理 特点 备份策略 二.环境准备 开启binlog 创建授权用户 安装innobackupex 三.添加脚本 全量备份 增量备份 bin ...
- (转)Zookeeper原理和作用
本周末学习zookeeper,原理和安装配置 本文参考: http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/ http:/ ...
- String类源码分析
1.String类注释说明 /** * The {@code String} class represents character strings. All * string literals in ...