python esay understand solution(two points)


  • 0
    class Solution(object):
        def thirdMax(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if len(set(nums))<3:
                return max(nums)
            nums=sorted(nums,reverse=True)
            # use two points to find unique 3th num
            i,j=1,1
            val=nums[0]
            while j < len(nums):
                if i<3:
                    if nums[j]==val:
                        j+=1
                    else:
                        nums[i]=nums[j]
                        val=nums[j]
                        j+=1
                        i+=1
                else:
                    break
            return nums[2]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.