当前位置:网站首页>LeetCode 414. The third largest number (simple, array) day13
LeetCode 414. The third largest number (simple, array) day13
2022-04-22 23:07:00 【White Code】
Title Description : 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 1 .
title :
Method 1 :TreeSet aggregate ( characteristic : Do not repeat , No index , Sortable )
1、 Make a statement TreeSet aggregate , It is characterized by non repetition 、 No index 、 Sortable ( The default order is from small to large );
2、 Traversal array , Add array elements to the collection , because TreeSet The properties of sets , Then duplicate elements will not be added to the collection , also
Judge whether the element in the set is greater than 3 individual , If more than three , Use remove Method to remove the first element of the set ,
Always keep the number of elements in the collection to 3 individual ( The premise is that the array elements should be greater than three , These three elements are the top three numbers in the set );
3、 Judge whether the number of set elements is three , If three return the first element , Otherwise, the tail element is returned .
Method 2 :ArrayList aggregate + Sort
1、 First, sort the array ( From small to small );
2、 Add array elements to ArrayList Set and de duplication ;
3、 here ArrayList The elements in the set are arranged from small to large , Return according to the number of elements in the set .
If you are not familiar with the application of sets, you can refer to the article :Java Medium Collection Assemble and Collection Collection implementation class instance https://blog.csdn.net/qq_43751200/article/details/123803851
Code implementation :
import java.util.*;
public class ThirdMax {
public static void main(String[] args) {
int[] nums = {-2147483648,1,1};
System.out.println(thirdMax1(nums));
}
// Method 1:TreeSet aggregate
public static int thirdMax(int[] nums){
// 1. Make a statement TreeSet aggregate , Be careful TreeSet The properties of sets , No repetition , No index , Sortable ( Automatic sorting from small to large )
TreeSet<Integer> set = new TreeSet<>(); // Default ascending order
// 2. Add elements from the array to the collection
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
if(set.size() > 3){
set.remove(set.first());
}
}
return set.size() == 3 ? set.first() : set.last();
}
// Method 1:ArrayList aggregate
public static int thirdMax1(int[] nums){
Arrays.sort(nums);
ArrayList<Integer> arrayList = new ArrayList<>();
// Traverse the array and remove the duplicates , Add array elements to the collection
for (int i = 0; i < nums.length; i++) {
if(arrayList.contains(nums[i])){
continue;
}else {
arrayList.add(nums[i]);
}
}
return arrayList.size() >= 3 ? arrayList.get(arrayList.size() - 3) : arrayList.get(arrayList.size() -1);
}
}
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/third-maximum-number
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/third-maximum-number
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source . There are two values in 2 Number of numbers , They're both second . The third largest of all the different numbers is 1 .
版权声明
本文为[White Code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222256039179.html
边栏推荐
猜你喜欢

C add log4net log (console and file) to console application

如何看待、快手、知乎等平台将上线显示用户IP属地功能?

Dart: use async and await in the loop

多线程-线程通信(wait-notify,await-single,park-unpark)

Multi thread thread communication (wait notify, await single, park unpark)

The article "fluent" is used to select and upload pictures

Flutter 一文搞定图片选择和图片上传

Golang中json.Marshal避坑

3. 源码

基于Multisim的波形发生器
随机推荐
多线程-线程通信(wait-notify,await-single,park-unpark)
Gentoo系统安装步骤详解
RPC详解
Cron表达式
L1-071 前世档案 (20 分)
L1-070 吃火锅 (15 分)
Go语言学习笔记——读锁重入导致死锁
Golang中json.Marshal避坑
Introduction to encryption mode (ECB, CBC, PCBC, CFB, OFB, CTR)
vtkVertex 顶点
Quantitative-c language implementation of equivalent domino pairs
RPC details
L1-069 胎压监测 (15 分)
Query uid from proc
Minio基本使用与原理
想让别人看你的视频?评论你的视频?学会这2招就够了
icy的mice profiler不支持的数据类型
3. 源码
解决require is not defined的报错问题
AcWing 1842. 牛奶桶 遍历所有情况