zoukankan      html  css  js  c++  java
  • 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).

    给一个数组,找到第三大的值,如果没有就返回最大值。这里的值是有并列的,

    [2, 2, 3, 1]->1而不是2
    class Solution(object):
        def thirdMax(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            d = {}
            for value in nums:
                d[value] = 1
            first_max = float('-inf')
            second_max = float('-inf')
            third_max = float('-inf')
            for key,value in d.items():
                if key > first_max:
                    third_max = second_max
                    second_max = first_max
                    first_max = key
                elif key > second_max:
                    third_max = second_max
                    second_max = key
                elif key > third_max:
                    third_max = key
            return third_max if third_max != float('-inf') else first_max
                
            
  • 相关阅读:
    flash
    应用缓存
    音频和视频
    拖拽借口
    地理定位接口
    表单元素
    jquery常见用法
    jquery ajax 模板
    Ubuntu下Lucene环境搭配
    小絮叨
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13302987.html
Copyright © 2011-2022 走看看