题目

描述:给定数组中求第三大的数字;如果没有,返回最大的;时间复杂度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的更多相关文章

  1. C#LeetCode刷题之#414-第三大的数(Third Maximum Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...

  2. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  3. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

  4. 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 ...

  5. 414 Third Maximum Number 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...

  6. LeetCode 414 Third Maximum Number

    Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...

  7. 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 ...

  8. 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...

  9. leetcode 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

随机推荐

  1. STM32 BootLoader升级固件

    一.知识点 1.BootLoader就是单片机启动时候运行的一段小程序,这段程序负责单片机固件的更新,也就是单片机选择性的自己给自己下程序.可以更新,也可以不更新,更新的话,BootLoader更新完 ...

  2. 扩展kmp 学习笔记

    学习了一下这个较为冷门的知识,由于从日报开始看起,还是比较绕的-- 首先定义 \(Z\) 函数表示后缀 \(i\) 与整个串的 \(lcp\) 长度 一个比较好的理解于实现方式是类似于 \(manac ...

  3. 看动画学算法之:二叉搜索树BST

    目录 简介 BST的基本性质 BST的构建 BST的搜索 BST的插入 BST的删除 简介 树是类似于链表的数据结构,和链表的线性结构不同的是,树是具有层次结构的非线性的数据结构. 树是由很多个节点组 ...

  4. A Child's History of England.48

    A few could not resolve to do this, but the greater part complied. They made a blazing heap of all t ...

  5. 修改 Gradle 插件(Plugins)的下载地址(repositories)

    Gradle 也可以用下面的方式声明使用的插件: 1234 // build.gradleplugins { id 'com.example.plugin', version '1.0'} 其实是从 ...

  6. EM配置问题

    配置EM,首先要保证dbconsole在运行. C:\Users\dingqi>emctl start dbconsoleEnvironment variable ORACLE_UNQNAME ...

  7. 运维笔记之yum,rpm,挂载,磁盘管理和raid详解

    yum 与 rpm centos6,7 主要有rpm和yum这两种包管理软件,两种包的管理各有用处,其中最主要区别是:  yum使用简单但需要联网,yum会去网上的yum包源去获取所需要的软件包.而r ...

  8. Spring boot 配置文件默认放置位置,和加载优先级

    一 .默认配置文件目录 spring boot 启动会扫描以下位置的application.properties 或者application.yml文件作为spring boot 的默认配置文件 ,加 ...

  9. lucene的索引查询

    package com.hope.lucene;import org.apache.lucene.document.Document;import org.apache.lucene.document ...

  10. 基于Annotation(注解)的装配

    一.常用注解 1.@Component 是一种通用注解,可用于任何Bean 2.@Repository 通常用于注解DAO层类,即持久层 3.@Service 通常用于注解Service类,即服务层 ...