414. Third Maximum Number
程序员文章站
2022-03-07 19:44:49
...
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;
}
推荐阅读
-
Leetcode 1456. Maximum Number of Vowels in a Substring of Given Length (python)
-
【Leetcode】1072. Flip Columns For Maximum Number of Equal Rows(异或运算)
-
mac svn: E210004: Number is larger than maximum
-
mac svn: E210004: Number is larger than maximum
-
ORA-00020:maximum number of processes 不能停机怎么办
-
ORA-00020:maximum number of processes 不能停机怎么办
-
321. Create Maximum Number
-
#leetcode#321.Create Maximum Number
-
Leetcode 321. Create Maximum Number
-
【Leetcode】414.Third Maximum Number