python - Python无法理解循环中的条件语句
问题描述
在 python 中需要帮助。这段代码来自leetcode.com,这个问题的解决方案之一,无法理解那里的条件语句,正是代码“ 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
解决方案
它返回堆栈的最后一个索引中的第二个元素
例如,如果堆栈是一个字符串列表,例如
stack = ['abc','def','ghi']
比 stack[-1][1] 返回
stack[-1] <-- 'ghi'
stack[-1][1] <-- 'h'
推荐阅读
- php - 用于捕获重复单词之间的组的正则表达式
- ios - 如何在swift中突出显示文本时触发事件
- python - 在成功运行之前,无法让我的脚本继续尝试使用不同的代理
- c++ - 用于显示显示垃圾值的矩阵元素的 C++ 函数
- r - 如何将 Shiny 中的 TextInput 与另一个对象进行比较?
- powerbi - 根据列标题创建 Power BI Slicer
- jquery - 为什么我的 .cshtml 页面渲染 ViewComponent 不正确
- ios - 将 JSON 字符串转换为字典
将布尔值序列化为 Int - mysql - SQL语法不熟悉
- java - Hibernate Where 子句中的 SQLSyntaxErrorException