Leetcode739 - Daily Temperatures
题目描述
Leetcode 739 本题考察了栈的使用。题目输入是一段温度值列表,然后返回一个列表。这个列表包含了输入列表中每一天还有多少天温度升高。如果未来没有升高的情况,则输入 0。
# Example1:
# Input: T = [73, 74, 75, 71, 69, 72, 76, 73]
# Output: [1, 1, 4, 2, 1, 1, 0, 0]
# 比如 index=0 这天温度为 73 度,index=1,这天为 74 度。
# 所以针对 index=0 这天,还需要 1 天温度会升高。
# 对于 index=2 这天,还需要 4 天才可以温度升高。
题目分析
通常的做法是从某一位置开始依次和该位置之后的温度进行比较,但这样就会出现冗余的情况。
拿 index=2 的温度来说,会和 71,69,72,76 依次做比较。但实际上经过此次比较,71,69 已
经找到比它本身温度高的答案了,复杂度为 O(N^2)
可以使用栈进行保存,栈里面保存的是当前栈顶元素的下标。而当前遍历元素的下标减去栈顶元素的
下标就是所需要经历的天数。每个元素最多被弹出和压入栈一次,因此为 O(N).
# Question: Daily Temperatures
# Given a list of daily temperatures T, return a list such that, for each day in
# the input, tells you how many days you would have to wait until a warmer
# temperature. If there is no future day for which this is possible, put 0
# instead.
# Example1:
# T = [73, 74, 75, 71, 69, 72, 76, 73]
# [1, 1, 4, 2, 1, 1, 0, 0]
# Note:
# the length of temperatures will be in the range [1, 30000]
# Each temperature will be an integer in the range [30, 100]
class Solution(object):
def dailyTemperatures(self, T):
"""
:type T: List[int]
:rtype: List[int]
"""
stack = []
stack_sky = [0] * len(T)
for index, element in enumerate(T):
if stack.__len__() > 0:
tem = T[stack[-1]]
while stack and element > tem:
stack_sky[stack[-1]] = index - stack[-1]
stack.pop()
if stack.__len__() > 0:
tem = T[stack[-1]]
stack.append(index)
return stack_sky
if __name__ == '__main__':
example_list_1 = [73, 74, 75, 71, 69, 72, 76, 73]
example_list_2 = [89, 62, 70, 58, 47, 47, 46, 76, 100, 70]
solution = Solution()
print(solution.dailyTemperatures(example_list_2))
Leetcode739 - Daily Temperatures的更多相关文章
- [Swift]LeetCode739. 每日温度 | Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
- [LeetCode] Daily Temperatures 日常温度
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- [Leetcode 739]*还有几天会升温 Daily Temperatures
[题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...
- LeetCode - Daily Temperatures
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
- 69.Daily Temperatures(日常气温)
Level: Medium 题目描述: Given a list of daily temperatures T, return a list such that, for each day in ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
随机推荐
- 如何利用Wireshark解密SSL和TLS流量
如何利用Wireshark解密SSL和TLS流量https://support.citrix.com/article/CTX135121 1.有server端的private key,直接在wires ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- 【原创】go语言学习(十六)接口
目录 接口介绍与定义 空接口和类型断言 指针接收和值接收区别 接口嵌套 接口介绍与定义 1. 接口定义了一个对象的行为规范 A. 只定义规范,不实现B. 具体的对象需要实现规范的细节 2.Go中接口定 ...
- 解决Navicat无法连接到centos上的MySQL,但命令行可以,修改权限,MySQL密码权限受限:ERROR 1820 (HY000) ERROR 1819 (HY000)
问题分析 查看MySQL文档发现5.7版本后加入了对用户密码严格的管理规范,具体设置字段如下: validate_password_dictionary_file #插件用于验证密码强度的字典文件路径 ...
- IDEA2018全局搜索中搜索jar包/lib
搜索jar包 配置find in path ctrl+shift+f 点击 ...处 把lib添加进来 点击OK保存 之后在IDEA中 , 双击shift , 调出全局搜索框就可以搜索到 jar包里的 ...
- 一个类中域(field)的首字母不要大写
首先这种写法不规范, 其次,至少在AJAX交互的情况下, 如果首字母大写,会无法与前端相同名称的JSON属性相绑定. 如 data:{'Name':'2017-10-19'} public NameI ...
- Spring注解不生效
如果在使用spring中,发现注解不生效,检查下如下配置是否配置. 1:一般情况下@Autowired默认是不生效的,配置之后,才会生效 <context:annotation-config / ...
- elasticsearch启动错误
requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled java.lang.UnsupportedOpe ...
- HbuilderX
HBuilderX入门教程 HX是轻量编辑器和强大IDE的完美结合体.敏捷的性能,清爽的界面,强大的功能和于一身. 本文档面向入门用户,建议初学者仔细研读. 介绍 执着于更快一步的理念,hx的左侧项目 ...
- Thrift: Scalable Cross-Language Services Implementation
http://thrift.apache.org/static/files/thrift-20070401.pdf