当前位置:网站首页>Leetcode- third largest number - simple
Leetcode- third largest number - simple
2022-06-13 05:49:00 【AnWenRen】
title :414 The third largest number - Simple
subject
Give you a non empty array , Returns the value in this array The third largest number . If it doesn't exist , Then return the maximum number in the array .
Example 1
Input :[3, 2, 1]
Output :1
explain : The third largest number is 1 .
Example 2
Input :[1, 2]
Output :2
explain : The third largest number doesn't exist , So return the maximum number 2 .
Example 3
Input :[2, 2, 3, 1]
Output :1
explain : Be careful , Ask to return the third largest number , It's the third largest of all the different numbers .
There are two values in this example that are 2 Number of numbers , They're both second . The third largest of all the different numbers is
Tips
1 <= nums.length <= 104-231 <= nums[i] <= 231 - 1
Code Java
public int thirdMax(int[] nums) {
long first = Long.MIN_VALUE;
long second = Long.MIN_VALUE;
long third = Long.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
if (first < nums[i]) {
third = second;
second = first;
first = nums[i];
} else if (second < nums[i] && nums[i] < first) {
third = second;
second = nums[i];
} else if (third < nums[i] && nums[i] < second) {
third = nums[i];
}
}
if (third == Long.MIN_VALUE)
return (int) first;
return (int) third;
}
边栏推荐
- Sentinel series integrates Nacos and realizes dynamic flow control
- Leetcode- intersection of two arrays - simple
- August 15, 2021 another week
- Django uploads local binaries to the database filefield field
- MongoDB 多字段聚合Group by
- 2020 personal annual summary
- @Detailed explanation of propertysource usage method and operation principle mechanism
- 13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
- Nacos series registry principle and source code analysis
- Difference between deviation and variance in deep learning
猜你喜欢

Database design

Information collection for network security (2)

How slow is the application system on tongweb? How dead is it?

Nacos series registry principle and source code analysis

2021.9.30 learning log -postman

17 servicetask of flowable task

SPI primary key generation strategy for shardingsphere JDBC

MongoDB 多字段聚合Group by

1 Introduction to drools rule engine (usage scenarios and advantages)

Django uses redis to store sessions starting from 0
随机推荐
Basic application of sentinel series
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
Django uses redis to store sessions starting from 0
Leetcode- intersection of two arrays ii- simple
powershell优化之一:提示符美化
Working principle of sentinel series (concept)
Misunderstanding of tongweb due to ease of use
AUTOSAR实战教程pdf版
Agile conflicts and benefits
What happens when the MySQL union index ABC encounters a "comparison operator"?
Service architecture diagram of Nacos series
= = relation between int and integer
Exception after repeated application redeployment on tongweb: application instance has been stopped already or outofmemoryerror:metaspace
2021.9.29 learning log MIME type
Hump naming and underlining
Config server configuration center of Nacos series
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
Sentinel series introduction to service flow restriction
The reason why the process cannot be shut down after a spark job is executed and the solution
19 calling subprocess (callactivity) of a flowable task