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 ...
随机推荐
- Sybase 修改数据库默认排序
我新建了一个sybase数据库,想用dump文件load,可是报数据库的排序不对,就去Centrol里面修改,但是还是报错,说是字符集不存在.办法如下: 打开命令行,进入到sybase的ASE-15_ ...
- MySQL系列之三查询优化
通常来说,查询的生命周期大致可以按照顺序来看从客户端到服务端,然后在服务器上进行解析,生产执行计划, 执行,并返回结果给客户端.其中的执行阶段可以认为是整个生命周期中最重要的阶段,其中包括了大量为了检 ...
- phpstorm 开发php入门
环境:ubuntu phpstorm apache mysql 一.安装软件 安装apache服务器 https://i.cnblogs.com/EditPosts.aspx?postid=1113 ...
- 苹果推出了AI手机,打造一款高度个性化的设备
在今年苹果的WWDC 2018上,一些人认为今年会因为软件专注而缺乏新的MacBook和iPad而感到无聊,该公司宣布,iOS12的推出可能是迄今为止最重要的操作系统更新.一系列Siri增强功能,Ap ...
- Java web入门之Http请求和响应
三层架构 web层:JSP + Servlet.Struts 2.SpringMVC service层:Spring dao层:JDBC.DBUtils.Hibernate.MyBatis form表 ...
- JPA @Id 和 @GeneratedValue 注解详解
JPA @Id 和 @GeneratedValue 注解详解 @Id: @Id 标注用于声明一个实体类的属性映射为数据库的主键列.该属性通常置于属性声明语句之前,可与声明语句同行,也可写在单独行上 ...
- gensim word2vec |来自渣渣硕的学习笔记
最近写论文跑模型,要用到word2vec,但是发现自己怎么也看不懂网上的帖子,还是自己笨吧,所以就有了我的第一篇博客!!! 关于word2vec工具打算写一个系列的,当然今天这篇文章只打算写: 如何 ...
- 支付宝证书签名 PHP SDK
PHP 接入支付宝证书方式签名以及验签 支付宝在 2019.10.25 日左右更新了新的 PHP SDK (v4.1.0). 之前的 PHP SDK(v3.4.2) 仅支持公钥方式加签.这次更新之后 ...
- 【Shiro】SpringBoot集成Shiro
项目版本: springboot2.x shiro:1.3.2 Maven配置: <dependency> <groupId>org.apache.shiro</grou ...
- Python爬虫之抓取豆瓣影评数据
脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...