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 ...
随机推荐
- 四十一.redis主从复制 RDB/AOF持久化 数据类型
把redis集群里的主机 恢复为独立的redis服务器(51-58) ]# redis-cli -h 192.168.4.51 -p 6351 shutdown ]# rm -rf /var/lib ...
- MVC 元数据验证
ASP.NET MVC 3 Validation - 正则表达式验证RegularExpressionAttribute之日期验证 http://blog.csdn.net/jackvs/articl ...
- Ubuntu下Django+uWSGI+nginx部署
本文采用uwsgi+nginx来部署django 这种方式是将nginx作为服务端前端,将接受web所有的请求,统一管理,Nginx把所有的静态请求自己处理,然后把所有非静态请求通过uwsgi传递给D ...
- Codeforces Global Round 4
目录 Contest Info Solutions A. Prime Minister B. WOW Factor C. Tiles D. Prime Graph E. Archaeology F1. ...
- 【java8新特性】日期和时间
Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程,新的 JavaScript ...
- js 返回两数(包含这两数)之间的随机数函数
function selectFrom( lowerValue, upperValue ){ var choices = upperValue - lowerValue + 1; return Mat ...
- Multiple commands produce "*.framework"
参考链接记录个问题,这是xcode10后新build系统导致的,新系统帮我们检查了很多东西,最优化我们的构建, 两种方案 1.用旧的系统(不推荐) 2.这个是build setting->b ...
- arcgis python 发送邮件
import arcgisscripting, smtplib, os, sys, traceback from email.MIMEMultipart import MIMEMultipart fr ...
- 【SpringBoot/MVC】从Oracle下载百万条记录的CSV
工程下载地址:https://files.cnblogs.com/files/xiandedanteng/CsvDownloadOracle20191110-2.rar 画面: 核心代码: 控制器: ...
- SQL优化 | Oracle 绑定变量
之前整理过一篇有关绑定变量的文章,不太详细,重新补充一下. Oracle 绑定变量 http://www.cndba.cn/Dave/article/1275 一.绑定变量 bind variable ...