414. Third Maximum Number

一种基于机载LiDAR点云电力线自动提取方法 文章目录一、引言二、算法实现2.1实现步骤2.2实现代码 一、引言 二、算法实现 2.1实现步骤 (1)首先采用首先采用高程滤波方法去除地面点和低矮地物点,滤波后的点云数据中除电力线点云外还含部分杆塔点、高植被点和噪声点。 (2)根据点云空间维度特征提取电力线点,使用空间密度聚类的方法实现单根电力线精确分离。 2.2实现代码 在这里插入代码片 ... 阅读详情

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: [3, 2, 1]

Output: 1

Explanation: The third maximum is 1.

Example 2:

Input: [1, 2]

Output: 2

Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: [2, 2, 3, 1]

Output: 1

Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.

法1:使用快排中的“切分” O(NlgN)

public class Solution {
    public int thirdMax(int[] nums)
	{
        
		HashSet<Integer> hashset=new HashSet<>(nums.length);
        for(int n : nums)
        	hashset.add(n);
        
        int[] arr=new int[hashset.size()];
        int cnt=0;
        for(int n :hashset)
        	arr[cnt++]=n;
        
        int len=arr.length;
        
        if(len<3)
        {
        	int max=Integer.MIN_VALUE;
        	for(int i=0;i<len;i++)
        		max=Math.max(arr[i], max);
        	return max;
        }
        
       
        
       return findkth(arr, len-3, 0, len-1);
        
    }
	
	public int findkth(int[] nums,int k,int lo,int hi)
	{
		int kth=partition(nums, lo, hi);
		int ele=nums[kth];
		
		if(kth>k)
			return findkth(nums, k, lo, kth);
		else if(kth<k)
			return findkth(nums, k, kth+1, hi);
		else {
			return ele;
		}
	}
	
	public int partition(int[] nums,int lo,int hi)
	{
		if(lo==hi)
			return lo;
		
		int num=nums[lo];
		int i=lo,j=hi+1;
		while(true)
		{
			while(nums[++i]<num)
				if(i==hi)
					break;
			while(nums[--j]>num)
				if(j==lo)
					break;
			if(i>=j)
				break;
			
			swap(nums, i, j);
		}
		
		swap(nums, lo, j);
		
		return j;
	}
	public static void swap(int[] arr ,int i, int j)
	{
		int temp=arr[i];
		arr[i]=arr[j];
		arr[j]=temp;
	}
}


法2 ,使用三个变量直接检索,摘自

https://discuss.leetcode.com/topic/62236/java-solution-in-0ms-run-time-o-n-and-space-o-1/15


public int thirdMax(int[] nums) {
    long max = Long.MIN_VALUE, mid = max, min = max;
        
    for (int ele : nums) {
        if (ele > max) {
            min = mid;
            mid = max;
            max = ele;
        } else if (max > ele && ele > mid) {
            min = mid;
            mid = ele;
        } else if (mid > ele && ele > min) {
            min = ele;
        }
    }
        
    return (int)(min != Long.MIN_VALUE ? min : max);
}

人工智能智能体研究综述:从理论架构到前沿应用 AI Agent,即人工智能智能体,是指能够在特定环境中自主感知、推理、决策并执行行动以实现特定目标的智能系统[1]。自主性反应性主动性(Pro-activeness)和社会性fill:#333;important;important;fill:none;color:#333;color:#333;important;fill:none;fill:#333;height:1em;AI Agent核心特征自主性 Autonomy反应性 Reactivity主动性 Pro-activeness。 阅读详情

相关推荐

UDP协议详解——使用Socket通信

UDP(UserDatagramProtocol)是一个简单的面向消息的传输层协议,尽管UDP提供标头和有效负载的完整性验证(通过校验和),但它不保证向上层协议提供消息传递,并且UDP层在发送后不会保留UDP 消息的状态。因此,UDP有时被称为不可靠的数据报协议。如果需要传输可靠性,则必须在用户应用程序中实现。UDP使用具有最小协议机制的简单无连接通信模型。UDP提供数据完整性的校验和,以及用于在数据报的源和目标寻址不同函数的端口号。它没有握手对话,因此将用户的程序暴露在底层网络的任何不可靠的方面。

qq_3517289697的博客 3385

Leetcode414. Third Maximum Number

@小花 刚开始写一些leetcode的一些笔记,并没有什么经验。 关于题目的描述如下: 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 compl

无忌的博客 2396

从淬火实验到仿真验证:用ANSYS APDL复现钢球冷却温度变化(含动画生成教程)

本文详细介绍了如何利用ANSYS APDL进行淬火过程的瞬态热分析仿真,以复现钢球冷却温度变化。内容涵盖从实验数据校准材料参数、构建高保真模型、设置非线性瞬态求解,到提取节点温度曲线和生成温度云图动画的全流程。通过APDL命令流实现自动化,为工程研发和学术研究提供高效、可靠的仿真验证方法。

pz8901234的博客 791

LeetCode 414. Third Maximum Number. C++

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). Input: [3, 2, 1] Output: 1 Explanation: The third maximu

Hello 1470

手把手教你用差分放大电路搭建LC振荡器(实测频率误差分析)

本文通过一个差分放大电路搭建LC振荡器的实践案例,深入分析了实测频率与理论值产生显著误差的原因。核心在于共模电感的互感效应改变了谐振回路的等效电感,导致频率偏差。文章提供了从电路搭建、误差根源剖析到波形优化与稳定性提升的完整解决方案,帮助电子爱好者深入理解模拟电路的实际行为。

svm4gardener的博客 541

微信小程序实现锚点效果 scroll-view的scroll-into-view属性

小程序中实现锚点效果,可以直接使用scroll-view的scroll-into-view属性就可以实现锚点效果,比较方便简单。那么需要用到scroll-view那些参数呢,下面具体讲讲:效果展示: wxml **注意:**viewid值是动态的,通过setData设置即可。js wxss......

FaxMiao的博客 5449

【leetcode】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: [3, 2, 1]...

ghscarecrow的博客 206

GEE开发之Landsat8计算MNDWI和数据分析

上一篇博客介绍了landsat8计算NDWI以及数据的获取,这次介绍MNDWI(改进的归一化差异水体指数)。

等待着冬天的风的博客 8440

414. Third Maximum Number (java)

414. Third Maximum Number DescriptionSubmissionsSolutions Total Accepted: 27406Total Submissions: 100537Difficulty: EasyContributors: ZengRed , 1337c0d3r Given a non-empty array

ysCover 1109

如何利用CANoe在两路CAN通道之间创建网关(gateway)

CANoe网关,利用CAPL实现CAN转发

Richy_Lai的博客 7897

Python爬虫实战:如何爬取汽车之家报价信息

本文通过实战介绍了如何使用Python编写爬虫,抓取汽车之家网站的车型、价格、配置等信息。在爬虫实现过程中,我们使用了requests库来发送HTTP请求,使用解析HTML页面,提取嵌入的JSON数据,并将数据存储到CSV文件中。同时,我们还介绍了如何使用反爬虫策略(如随机User-Agent、请求延时、代理池等)来规避反爬虫机制,确保爬虫的稳定性。

2201_76125261的博客 1594

LeetCode 414.Third Maximum Number

LeetCode 414.Third Maximum NumberDescription: 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

My Blog 365

EMB电子机械制动器夹紧力预估策略分析

本文探讨了电子机械制动器(EMB)夹紧力的四种预估方法:1)基于电机电流的方法简单经济但易受干扰;2)基于位置传感器的方法实时性好但依赖模型精度;3)直接力测量精度最高但成本较高;同时提出未来将结合AI和数字孪生技术提升预估精度。研究显示,准确的夹紧力预估对制动安全性、能效和舒适性至关重要,当前面临温度稳定性、参数漂移等技术挑战。随着智能算法的发展,EMB夹紧力预估将实现更高精度和可靠性。

m0_59012156的博客 851

LeetCode 414. Third Maximum Number

414. Third Maximum Number (第三大的数) 链接:      https://leetcode-cn.com/problems/third-maximum-number/ 题目:      给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。   示例 1:   输入: [3, 2, 1]...

weixin_30522183的博客 207

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

[leetcode] 414. Third Maximum Number 解题报告

题目链接:https://leetcode.com/problems/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 t

小榕流光的专栏 2186

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

[C语言][LeetCode][414]Third Maximum Number

[C语言][LeetCode][414]Third Maximum Number

Timsley的专栏 1408

[leetcode] 414. Third Maximum Number

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: ...

Learning from the mistakes 190

LeetCode //C - 414. Third Maximum Number

【代码】LeetCode //C - 414. Third Maximum Number。

Made in Code 864
上一篇: 413. Arithmetic Slices
下一篇: 415. Add Strings
wdlsjdl2
博客等级 码龄15年 4粉丝 246原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值