需要python方面的帮助。这段代码来自leetcode.com,这是problem的解决方案之一,无法理解那里的条件语句,正是代码“stack[-1][1]
”
class Solution(object):
def dailyTemperatures(self, temps):
if not temps:
return []
result = [0] * len(temps)
stack = []
for curr_idx, curr_temp in enumerate(temps):
while stack and curr_temp > stack[-1][1]: # not clear, and I know, it is not a type of access to list element
last_idx, last_temp = stack.pop()
result[last_idx] = curr_idx - last_idx
stack.append((curr_idx, curr_temp))
return result
它返回堆栈最后一个索引中的第二个元素
例如,如果堆栈是字符串列表
然后堆栈[-1][1]返回
相关问题 更多 >
编程相关推荐