day14-栈练习题力扣496

day14-栈练习题力扣496_第1张图片
Python代码:

def nextGreaterElement(self, findNums, nums):
    """
    :type findNums: List[int]
    :type nums: List[int]
    :rtype: List[int]
    """
    st = []
    dic = {}        
    for num in nums:
        while len(st) and st[-1]<num:
            dic[st.pop()] = num
        st.append(num)
    return [dic.get(x,-1) for x in findNums]

你可能感兴趣的:(leetcode,算法,职场和发展)