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
我的前方是万里征途,星辰大海!!
原文地址:https://www.cnblogs.com/taoyuxin/p/11783138.html