您的位置:首页 > 其它

leetcode_414. Third Maximum Number 返回数组中第三大的数,要求用O(n)的时间复杂度

2016-11-06 20:37 253 查看
题目:

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.


题意:

给定一个非空整数数组,返回这个数组第三大的数,如果第三大的数不存在,则返回最大的数。要求用O(n)的时间复杂度。相同的数,表示一样大。

代码:

class Solution(object):

    def thirdMax(self, nums):

        """

        :type nums: List[int]

        :rtype: int

        """

        

        n = len(nums)

        max_num = nums[0]      #记录最大的数,初始化为nums[0] 

        count = 1                    #记录最大的数的个数,用于寻找前三个最大的数

        

        indx = 1            #从下标1开始遍历

        while indx < n and count < 2 :            #寻找第二大的数mid_num

           if nums[indx] > max_num :

               mid_num = max_num

               max_num = nums[indx]

               count += 1

           else :

               if nums[indx] < max_num :

                   mid_num = nums[indx]

                   count += 1

           indx += 1

        

        while indx < n and count < 3 :               #继续寻找第三大的数min_num。当count=3时,表示前三个最大的数都找到,退出循环,开始遍历数组其他元素

           if nums[indx] > max_num :

               min_num = mid_num

               mid_num = max_num

               max_num = nums[indx]

               count += 1

           else :

               if nums[indx] < max_num and nums[indx] > mid_num :

                   min_num = mid_num

                   mid_num = nums[indx]

                   count += 1

               else :

                   if nums[indx] < mid_num :

                       min_num = nums[indx]

                       count += 1

           indx += 1

  

        for i in range(indx,n) :           #开始遍历数组其他元素。之前在这个地方犯过错,让i从indx+1开始遍历,其实是不对的。因为上面while跳出循环时,如果是indx>=n,则此处也不用遍历了,所以一定是count=3条件才会执行此处代码,此时,i = indx 的元素还没比较,故应该从indx开始遍历

            if nums[i] > max_num :

                min_num = mid_num

                mid_num = max_num

                max_num = nums[i]

            else :

                if nums[i] < max_num and nums[i] > mid_num :

                    min_num = mid_num

                    mid_num = nums[i]

                else :

                    if nums[i] < mid_num  and nums[i] > min_num :

                        min_num = nums[i]

        

        if count < 3 :         #当count小于3,表示没有第三大的元素

            return max_num

        else :

            return min_num

            

笔记:

这个题关键在初始化max_num,mid_num,min_num,代码用count变量(记录找到最大数的个数)来进行控制,依次定义mid_num和min_num。

表示效率还可以吧:

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐