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. ESP8266物联网开发 一

    其实学了这么多,最终目的还是在于物联网. 好吧,我们就在这个的基础上来吧,先摸索,边学边摸索. 去网上买了8266开发板... 遇到的问题: USB线的问题.从同事那随便搞了一根USB线,然后写好程序 ...

  2. LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...

  3. LOJ6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡! 最短路+bitset

    题目传送门 https://loj.ac/problem/6252 https://lydsy.com/JudgeOnline/problem.php?id=5109 题解 首先跑最短路,只保留 \( ...

  4. ubuntu下安装pyaudio

    首先安装依赖库,不安装依赖库会安装失败: sudo apt-get install libasound-dev portaudio19-dev libportaudio2 libportaudiocp ...

  5. 迅捷路由器FW325R的无线桥接

    1.恢复路由器默认设置 长按路由器后面的按钮直到指示灯全亮后只剩一个灯亮时松开按钮,此时就已经重置路由器了.然后设置路由器后台密码什么的,那些向导什么的可以跳过 2.高级设置内容 进入路由器高级设置: ...

  6. Codeforces 803F - Coprime Subsequences(数论)

    原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...

  7. Android中实现Activity的启动拦截之----实现360卫士的安装应用界面

    第一.摘要 今天不是周末,但是我已经放假了,所以就开始我们的技术探索之旅,今天我们来讲一下Android中最期待的技术,就是拦截Activity的启动,其实我在去年的时候,就像实现这个技术了,但是因为 ...

  8. 【Gitlab】git clone http连接,带用户名和密码

    test项目在gitlab的http请求的url: http://gitlab.com/test.git 用以下方式请求不需要再输入用户名和密码 git clone  http://username: ...

  9. window安装activemq

    原文: https://www.cnblogs.com/donsenChen/p/8656563.html ActiveMQ5.14.5下载地址 http://activemq.apache.org/ ...

  10. 如何利用nginx实现负载均衡(总结)

    如何利用nginx实现负载均衡(总结) 一.总结 一句话总结: 推荐使用nginx七层(应用层)负载均衡的实现:配置那是相当的简单 1.nginx配置实例? |||-begin #这里的域名要和下面p ...