欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

414. Third Maximum Number

程序员文章站 2022-03-07 19:44:49
...

414. Third Maximum Number
414. Third Maximum Number
414. Third Maximum Number

    int thirdMax(vector<int>& nums) {
       set<int> s;
        for(int a:nums) s.insert(a);
        int res=0;
        for(auto it=s.rbegin();it!=s.rend();++it){
            if(res==2)
                return *it;
            res++;
        }
        return *(s.rbegin());
    }
    int thirdMax(vector<int>& nums) {
        long first=LONG_MIN,second=LONG_MIN,third=LONG_MIN;
        for(auto a : nums){
            if(a==first||a==second)
                continue;
            if(a>first)
            {
                third=second;
                second=first;
                first=a;
            }else if(a>second){
                third=second;
                second=a; 
            }else if(a>=third)
                third=a;    
        }
        return third==LONG_MIN?first:third;
    }