414 Third Maximum Number 第三大的数
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
示例 1:
输入: [3, 2, 1]
输出: 1
解释: 第三大的数是 1.
示例 2:
输入: [1, 2]
输出: 2
解释: 第三大的数不存在, 所以返回最大的数 2 .
示例 3:
输入: [2, 2, 3, 1]
输出: 1
解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。
存在两个值为2的数,它们都排第二。
详见:https://leetcode.com/problems/third-maximum-number/description/
Java实现:
class Solution {
    public int thirdMax(int[] nums) {
        long first=Long.MIN_VALUE;
        long second=Long.MIN_VALUE;
        long third=Long.MIN_VALUE;
        for(int num:nums){
            if(first<num){
                third=second;
                second=first;
                first=num;
            }else if(num>second&&num<first){
                third=second;
                second=num;
            }else if(num>third&&num<second){
                third=num;
            }
        }
        return (third==Long.MIN_VALUE||third==second)?(int)first:(int)third;
    }
}
C++实现:
class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;
        for (int num : nums)
        {
            if (num > first)
            {
                third = second;
                second = first;
                first = num;
            }
            else if (num > second && num < first)
            {
                third = second;
                second = num;
            }
            else if (num > third && num < second)
            {
                third = num;
            }
        }
        return (third == LONG_MIN || third == second) ? first : third;
    }
};
参考:https://www.cnblogs.com/grandyang/p/5983113.html
414 Third Maximum Number 第三大的数的更多相关文章
- [LeetCode] Third Maximum Number 第三大的数
		
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
 - Leetcode414Third 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 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 ...
 - 414. Third Maximum Number数组中第三大的数字
		
[抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...
 - 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 Array Easy 414. Third Maximum Number
		
Description Given a non-empty array of integers, return the third maximum number in this array. If i ...
 - [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 ...
 
随机推荐
- iOS用户体验之-modal上下文
			
iOS用户体验之-modal上下文 何为模态视图,它的作用时聚焦当前.获得用户的注意,用户仅仅有完毕模态的任务才 退出模态视图.否则你将不能运行app的任务,比如,alert view,model v ...
 - [MAT]使用MAT比較多个heap dump文件
			
使用MAT比較多个heap dump文件 调试内存泄露时,有时候适时比較2个或多个heap dump文件是非常实用的.这时须要生成多个单独的HPROF文件. 以下是一些关于怎样在MAT里比較多个hea ...
 - Scrum 时间估算
			
在新公司里,不懂软件工程的产品经理经常逼迫研发人员作出很不靠谱的时间估算.常见场景有下面这些: 需求未细化的情况下要求给出时间估算:比如,就一句话描述需要做一个什么样的功能,但是具体页面长什么样,交互 ...
 - LoadRunner系列之—-01 接口压力测试脚本
			
LoadRunner中一般用如下函数进行接口测试: <一>. http或soap协议下的get请求接口,样例如下: web_url("integrated_query.jsp&q ...
 - 李洪强iOS开发之录音和播放实现
			
李洪强iOS开发之录音和播放实现 //首先导入框架后,导入头文件.以下内容为托控件,在storyboard中拖出两个按钮为录音和播放按钮 //创建一个UIViewController在.h文件中写 # ...
 - Windows下编译DCMTK
			
原帖地址:http://www.cnblogs.com/yinxufeng/p/3636241b7084b0340cc56fd37f9e2fd8.html 下载源码生成VS项目工程编译源码 下载源码 ...
 - SQL语句--常用
			
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
 - I.MX6 DNS 查看、修改方法
			
/************************************************************************** * I.MX6 DNS 查看.修改方法 * 说明 ...
 - poj 3517(约瑟夫环问题)
			
And Then There Was One Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4873 Accepted: ...
 - vue 练习 bug
			
在使用vue slot分发内容时,如果要绑定事件,不能绑定在slot元素上,同样的不能绑定在自定义元素的模板上,只能绑定在html 元素上,才会生效 demo <my-component v-o ...