推荐这个视频动画详解,看后就明白了:https://www.bilibili.com/video/av498397425/
class Solution:
def temperatures(self,tem_arr):
tem_arr_len = len(tem_arr)
res = [0]*tem_arr_len
stack = []
for i in range(tem_arr_len):
temp = tem_arr[i]
while stack and temp > tem_arr[stack[-1]]:
top_index = stack.pop()
res[top_index] = i - top_index
stack.append(i)
return res



京公网安备 11010502036488号