LeetCode 414. Third Maximum Number (第三大的数)

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.

 


题目标签:Array

  方法1:

  题目给了我们一个nums array, 让我们找到第三大的数。其中重复的数字不算。如果是要找一个max 的数,就很简单,现在是找第三个,那么就需要维护三个max1,max2,max3。

  每次找到一个数字比 max1 大的, 或者比max2 大的,或者比max3大的,就需要重新更新三个 max 的值。

  还有一点是,这里max1 max2 max3 要用long,因为用int Integer.MIN_VALUE 的话,如果array 中有一个 是 int 里最小的数字, 那么在最后判断max3 是有数字 还是没有的情况下,就不能分辨了。

 

Java Solution:

Runtime beats 97.74% 

完成日期:05/09/2017

关键词:Array

关键点:维护max1,max2,max3

 1 public class Solution 
 2 {
 3     public int thirdMax(int[] nums) 
 4     {
 5         long max1, max2, max3;
 6         max2 = Long.MIN_VALUE;
 7         max3 = max2;
 8         
 9         max1 = nums[0];
10         
11         for(int i=1; i<nums.length; i++)
12         {
13             int curNum = nums[i];
14             if(curNum > max1) // if find the first maximum number
15             {
16                 max3 = max2;    // copy max2's value into max3.
17                 max2 = max1;    // copy max1's value into max2.
18                 max1 = curNum;    // copy new value into max1.
19             }
20             else if(curNum > max2) // if find the second maximum number
21             {
22                 if(curNum != max1)
23                 {
24                     max3 = max2;    // copy max2's value into max3.
25                     max2 = curNum;    // copy new value into max2.    
26                 }    
27             }
28             else if(curNum > max3) // if find the third maximum number
29             {
30                 if(curNum != max2)
31                     max3 = curNum; // copy new value into max3.
32             }
33         }
34         
35         
36         if(max3 == Long.MIN_VALUE)    // meaning there is no third maximum value.
37             return (int)max1;
38         else
39             return (int)max3;
40     }
41 }

参考资料:N/A

 

  方法2:

  想法一样,不过运用了Integer 可以利用null,不需要把max = MIN_VALUE, 过程也比较简单清楚。

  

Java Solution:

Runtime beats 50.71% 

完成日期:09/19/2017

关键词:Array

关键点:维护max1,max2,max3

 1 public class Solution 
 2 {
 3     public int thirdMax(int[] nums) 
 4     {
 5         /* Solution 2: */
 6         Integer max1 = null;
 7         Integer max2 = null;
 8         Integer max3 = null;
 9         
10         for(Integer num : nums)
11         {    // skip duplicate number
12             if(num.equals(max1) || num.equals(max2) || num.equals(max3))
13                 continue;
14             
15             if(max1 == null || num > max1)
16             {
17                 max3 = max2;
18                 max2 = max1;
19                 max1 = num;
20             }
21             else if(max2 == null || num > max2)
22             {
23                 max3 = max2;
24                 max2 = num;
25             }
26             else if(max3 == null || num > max3)
27                 max3 = num;
28         }
29         
30         return max3 == null ? max1 : max3;
31     }
32 }

参考资料:

https://discuss.leetcode.com/topic/63715/java-neat-and-easy-understand-solution-o-n-time-o-1-space

 

LeetCode 题目列表 - LeetCode Questions List

 

posted @ 2017-09-20 04:30  Jimmy_Cheng  阅读(275)  评论(0编辑  收藏  举报