LeetCode 414. 第三大的数

题目链接:https://leetcode-cn.com/problems/third-maximum-number/

给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。

 1 int thirdMax(int* nums, int numsSize){
 2     long first = LONG_MIN,second = LONG_MIN,third = LONG_MIN;
 3     for(int i = 0;i < numsSize;i++){
 4         if(nums[i] > first){
 5             first = nums[i];
 6         }   
 7     }
 8     for(int i = 0;i < numsSize;i++){
 9         if(nums[i] > second && nums[i] < first){
10             second = nums[i];
11         }
12     }
13     for(int i = 0;i < numsSize;i++){
14         if(nums[i] > third && nums[i] < second && nums[i] < first){
15              third =nums[i];
16         }
17     } 
18     if(third != LONG_MIN){
19         return third;
20     }
21     return first;
22 }
原文地址:https://www.cnblogs.com/shixinzei/p/12145473.html