# (Easy) Third Maximum Number LeetCode

#### 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: [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.
```  #### Solution: ```class Solution {
public int thirdMax(int[] nums) {
if(nums==null||nums.length<=0){
return 0;
}

Arrays.sort(nums);
int max_real =  nums[nums.length-1];
int max = nums[nums.length-1];

int k = 3;
int third=0;

for( int i =nums.length-1; i>=0 && k>=1; i--){
System.out.println(i);
if(nums[i]<max){
max = nums[i];
k=k-1;

}

if(k ==1){

third = nums[i];
return third;
}

}

return max_real;
}
}```

### 最新文章

##### 2019-12-13

17-Python执行JS代码--PyExecJS、PyV8、Js2Py

##### 2019-12-13

Apache Maven从入门到升天

##### 2019-12-13

AQS系列（二）- ReentrantLock的释放锁

##### 2019-12-12

Spring Cloud第五篇 | 服务熔断Hystrix

##### 2019-12-12

CSS3(1)---圆角边框、边框阴影

### 热门文章

##### 2019-08-09

python itertools 模块讲解

##### 2019-07-03

52、沉浸式学 Git - 高级/将来的主题

##### 2019-09-12

Spring源码分析之-加载IOC容器