题目参见 leetcode

 1          d = {}
 2          st = []
 3          ans = []
 4           
 5          for x in nums:
 6              while len(st) and st[-1] < x:
 7                  d[st.pop()] = x
 8              st.append(x)
 9  
10          for x in findNums:
11              ans.append(d.get(x, -1))
12              
13          return ans

以上为stack 用python实现,在while循环中,一直在找位于x后的greater number,如果找到了就pop出去然后塞进hash表(以dict的形式);如果当前x不满足,那么就不管,放在stack中静静等待,总会有一个x大于stack[-1]。积压在stack中的元素依次递减,也就是说stack[-1]是最小的。

while循环会让stack中凡是小于x的元素都pop出去然后dict。

stack的运用和下面这个题目很像

654. Maximum Binary Tree