414. Third Maximum Number数组中第三大的数字
[抄题]:
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Example 1:
Input: [3, 2, 1] Output: 1 Explanation: The third maximum is 1.
Example 2:
Input: [1, 2] Output: 2 Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: [2, 2, 3, 1] Output: 1 Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
[暴力解法]:
时间分析:nlgn
空间分析:
[优化后]:
时间分析:n
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
不允许排序,就只能一个个地放了
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
用else if :

[一刷]:
- 去重复的方法:用continue继续处理,好像很少用。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
同时判断要用else if
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 不能初始化为0,就包装一下,初始化为null
- 或者初始化为极值
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
class Solution {
public int thirdMax(int[] nums) {
//ini
Integer max1 = null;
Integer max2 = null;
Integer max3 = null;
//for loop, change
for (Integer n : nums) {
if (n.equals(max1) || n.equals(max2) || n.equals(max3)) continue;
if (max1 == null || n > max1) {
max3 = max2;
max2 = max1;
max1 = n;
}
else if (max2 == null || n > max2) {
max3 = max2;
max2 = n;
}
else if (max3 == null || n > max3) {
max3 = n;
}
}
//return
return (max3 == null) ? max1 : max3;
}
}
[代码风格] :
414. Third Maximum Number数组中第三大的数字的更多相关文章
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- 414 Third Maximum Number 第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- [Array]414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- 4.产生10个1-100的随机数,并放到一个数组中 (1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。 (2)把数组中的数字放到当前文件夹的numArr.txt文件中
package cn.it.text; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayLis ...
- 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现
原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...
- C语言:一个数组中只有两个数字是出现一次
//1.一个数组中只有两个数字是出现一次, //其他所有数字都出现了两次. //找出这两个数字,编程实现.a //^=单独两个数的^结果 //单独出现的两个数不同位的标记 //position: ^结 ...
随机推荐
- nodejs express project
user root install express npm install express -g install express... npm install express-generator -g ...
- 如何用 php 读取一个很大的 excel 文件。
这个程序是用php 读取一个很大的excel文件, 先将 excel 文件保存成csv 文件, 然后利用 迭代器 逐行读取 excel 单元格的值, 拿到值以后 做相应处理,并打印结果. <?p ...
- 【解题报告】[动态规划]-PID69 / 过河卒
原题地址:http://www.rqnoj.cn/problem/69 解题思路: 用DP[i][j]表示到达(i,j)点的路径数,则 DP[0][0]=1 DP[i][j]=DP[i-1][j]+D ...
- Django的CSRF机制
原文链接:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 必须有的是: 1.每次初始化一个项目时,都能看到django.mi ...
- 12C 新特性--全库缓存
Force Full Database Caching Mode 意思就是可以把整个数据库缓存到内存中,当然你内存一定要非常大,起码要等于数据库的大小,才能容下整个数据库. 在RAC环境下,对于一个良 ...
- 通过ssh连接git操作
项目又一次从svn切换到git,而且要求使用安全协议ssh连接git.不管是个人还是组织,git上只负责导入公钥,而不会帮你生成.这里先说下怎么生成这个ssh的私钥和公钥. 首先你得找到有ssh命令的 ...
- 73个word使用终极技巧
1.问:Word里边怎样设置每页不同的页眉?如何使不同的章节显示的页眉不同? 答:分节,每节可以设置不同的页眉.文件——页面设置——版式——页眉和页脚——首页不同 2.问:请问Word中怎样让每一章用 ...
- 【转】使用ant来调用Jmeter,并定制运行时参数
为了应对不同的运行需求(主要是不同的线程数),以及可能的变化(host ip),在nongui运行时我对ant build.xml进行了一些修改 1. log目录备份与运行前清除 <tstamp ...
- NoSuchBeanDefinitionException: No bean named 'shiroFilter' is defined
以前运行正常的项目,过了一段时间再运行时出问题,打开链接无反应,无法访问Tomcat,空白页面. 经检查发现,在Tomcat log中有报错: NoSuchBeanDefinitionExceptio ...
- PyYAML和configparser模块讲解
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...