LeetCode Array Easy 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: [, , ] Output: Explanation: The third maximum is .Example 2:
Input: [, ] Output: Explanation: The third maximum does not exist, so the maximum () is returned instead.Example 3:
Input: [, , , ] Output:Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
问题描述,给定要给非空数组,找出第三大的数,如果找不到则返回最大值。
思路:用三个数分别存最大 第二 第三大的值。但是由于数组中会出现int的最小边界值。所以干脆用long类型来存储前三个值。
public int ThirdMax(int[] nums) {
long max = long.MinValue;
long sec = long.MinValue;
long third = long.MinValue;
for(int i = ; i < nums.Length; i++){
int temp = nums[i];
if(temp > max){
third = sec;
sec = max;
max=temp;
}
else if(temp < max && temp >sec){
third = sec;
sec=temp;
}
else if(temp < sec && temp >=third){
third = temp;
}
}
return third > long.MinValue ? (int)third : (int)max;
}

LeetCode Array Easy 414. Third Maximum Number的更多相关文章
- 【leetcode❤python】 414. Third Maximum Number
#-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...
- 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 ...
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- [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 ...
- 414. Third Maximum Number数组中第三大的数字
[抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...
- LeetCode Array Easy 485. Max Consecutive Ones
Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...
随机推荐
- 阿里云centos下搭建vsftpd,被动模式出现的问题
最近计网课设要做一个ftp服务端,所以先在自己服务器搭一个来了解一下. 首先在默认情况下连接,227 Entering Passive Mode (192,168,*,*,227,175). 显示连接 ...
- weblogic下载
1.网址 https://edelivery.oracle.com/osdc/faces/SoftwareDelivery 2.信息
- 开源安全:PE分析
https://github.com/JusticeRage/Manalyze.git https://github.com/JusticeRage/Manalyze https://www.free ...
- redis JedisConnectionException: Could not get a resource from the pool
转载:https://blog.csdn.net/testcs_dn/article/details/43052585 产生此错误的原因通常是: 一.Redis没有启动: 我自己遇到一次这样的问题.汗 ...
- map简单用法
let familyNames = []; familyNames = res.Data.map(item=> { return item.ArgText });
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- Idea的几个常用的
sout+tab= "System.out.println()" ctrl+alt+v=生成当前对象的实例 ctrl+shift+enter="(真个是真的牛哦)直接 ...
- c++ 递归思想 阶乘
#include "stdio.h" #include "iostream" long fact(int n); int main() { int i; sca ...
- springmvc把对象放到session中
1:首先把创建的对象放到Map中, @RequestMapping("/testSession") public String testSession(Map<Stri ...
- LDD3 第11章 内核的数据类型
考虑到可移植性的问题,现代版本的Linux内核的可移植性是非常好的. 在把x86上的代码移植到新的体系架构上时,内核开发人员遇到的若干问题都和不正确的数据类型有关.坚持使用严格的数据类型,并且使用-W ...