Description

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的更多相关文章

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

  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. 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)

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

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

  8. 414. Third Maximum Number数组中第三大的数字

    [抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...

  9. LeetCode Array Easy 485. Max Consecutive Ones

    Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...

随机推荐

  1. 只用ipv6 两台机器共享文件夹, 局域网连接路径,共享文件夹路径中ipv6地址如何表示

    1. 首先要确认你的DNS服务器支持IPv6,一般是指网络中的路由. 2. 如果网络中没有路由,则直接在hosts文件中添加对方的IPv6地址与名字. 3. 利用UNC路径,把冒号修改为连字符并附加. ...

  2. vue不是内部或外部命令的解决方法

    1.在nodejs的安装目录下,找到vue.cmd,将此路径加到环境变量中,我是通过nvm管理node版本的,路径是C:\Users\hy\AppData\Roaming\nvm\v6.10.0,关闭 ...

  3. Codeforces Round #394 (Div. 2) - A

    题目链接:http://codeforces.com/contest/761/problem/A 题意:给定a个偶数,b个奇数,问是否能构成奇偶相间的阶梯.思路:a和b相差小于等于1即可构造出来.特判 ...

  4. Sass函数:数学函数-abs函数

    abs( ) 函数会返回一个数的绝对值. >> abs(10) 10 >> abs(-10) 10 >> abs(-10px) 10px >> abs( ...

  5. iView的Message提示框

    全局配置message main.js Vue.prototype.$Message.config({ top: 70, duration:3 }); Vue.prototype.$Message.c ...

  6. 【学习笔记】可持久化并查集(BZOJ3673)

    好久之前就想学了 然后今天恰巧一道题需要用到就学了 前置芝士 1.主席树[可持久化数组] 2.并查集 如果你掌握了前面两个那么这个东西你就会觉得非常沙茶.. 构造 可持久化并查集 = 主席树  + 并 ...

  7. SDOI2018凉凉记

    好久没有更博客了...因为我在颓废学习.. SDOI一轮结束了...我也该回来学地生了... 凉凉 ————————————————我是分割线———————————————— Day0 愉快感冒的Da ...

  8. 21.使用LinkedBlockingQueue模拟生产者与消费者

    import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import java.util.co ...

  9. 11.IPFS搭建及上传获取数据——2019年12月12日

    title: ipfs使用 date: "2019-09-26 10:17:16" tags: ipfs categories: 技术驿站 1.mac安装ipfs--使用npm工具 ...

  10. vs code 使用技巧整理

    快捷键 Ctrl + Shift + F:在文件夹中搜索; Ctrl + Shift + P:命令面板; Ctrl + Shift + T:重新打开 关闭的编辑页面; Ctrl+Shift+PgUp/ ...