第三大的数

414、第三大的数

java 复制代码
class Solution {
    public int thirdMax(int[] nums) {

        
        Arrays.sort(nums);
        int temp=nums[0];
        int ans=nums[0];
        int count = 0;
        // if(nums.length<3){
        //     return nums[nums.length-1];
        // }
        // else {
            for(int i=nums.length-1;i>0;i--){
                if (nums[i]>nums[i-1]){
                    if(count>=2) break;
                    count++;
                    temp=nums[i-1];
                }
            }
        // }

        if(count>=2) ans=temp;
        else ans=nums[nums.length-1];

            return ans;
        }
 }
相关推荐
aloha_78921 小时前
力扣hot100做题整理91-100
数据结构·算法·leetcode
Tiny番茄1 天前
31.下一个排列
数据结构·python·算法·leetcode
挂科是不可能出现的1 天前
最长连续序列
数据结构·c++·算法
_Aaron___1 天前
List.subList() 返回值为什么不能强转成 ArrayList
数据结构·windows·list
前端小L1 天前
动态规划的“数学之魂”:从DP推演到质因数分解——巧解「只有两个键的键盘」
算法·动态规划
RTC老炮1 天前
webrtc弱网-ReceiveSideCongestionController类源码分析及算法原理
网络·算法·webrtc
21号 11 天前
9.Redis 集群(重在理解)
数据库·redis·算法
码农多耕地呗1 天前
力扣146.LRU缓存(哈希表缓存.映射+双向链表数据结构手搓.维护使用状况顺序)(java)
数据结构·leetcode·缓存
晚枫~1 天前
数据结构基石:从线性表到树形世界的探索
数据结构
hadage2331 天前
--- 数据结构 AVL树 ---
数据结构·算法