Third Maximum Number
给一个数组, 找第三大的数, 如果不够三个就返回第一个…注意数组中的数字有相同的…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Solution { public int thirdMax(int[] nums) { Integer first = null; Integer second = null; Integer third = null; for(Integer n : nums) { if(n.equals(first) || n.equals(second) || n.equals(third)) continue; if(first == null || n > first){ third = second; second = first; first = n; } else if(second == null || n > second){ third = second; second = n; } else if(third == null || n > third) third = n; } if(third == null){ return first; } else return third; } } |