Python3 自排序容器

Python自带有排序容器SortedList, SortedDict, SortedSet

下面以SortedSet为例,leetcode414

from sortedcontainers import SortedSet

class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        s=SortedSet()
        for x in nums:
            s.add(x)
            if len(s)>3:
                s.pop(0)
        return s[0] if len(s)==3 else s[-1]

 

posted @ 2021-10-06 14:18  Kinghao0319  阅读(318)  评论(0)    收藏  举报