leetcode——503. 下一个更大元素 II

class Solution(object):
    def nextGreaterElements(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        if not nums:
            return
        if sorted(nums,reverse=True)==nums and len(set(nums))==len(nums):
            return [-1]+[nums[0]]*(len(nums)-1)
        stack=[]
        b=max(nums)
        for i in range(len(nums)):
            if nums[i] == b:
                stack.append(-1)
            else:
                j=i+1
                if j==len(nums):
                    j=0
                while j<len(nums):
                    if nums[j]>nums[i]:
                        stack.append(nums[j])
                        break
                    else:
                        j+=1
                        if j==len(nums):
                            j=0
        return stack
执行用时 :248 ms, 在所有 python 提交中击败了58.82%的用户
内存消耗 :13.6 MB, 在所有 python 提交中击败了34.65%的用户
 
——2019.11.2
posted @ 2019-11-02 17:11  欣姐姐  阅读(193)  评论(0编辑  收藏  举报