题目描述

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的更多相关文章

  1. [Swift]LeetCode739. 每日温度 | Daily Temperatures

    Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...

  2. [LeetCode] Daily Temperatures 日常温度

    Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...

  3. [Leetcode 739]*还有几天会升温 Daily Temperatures

    [题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...

  4. LeetCode - Daily Temperatures

    Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...

  5. LeetCode 739. Daily Temperatures

    原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...

  6. LeetCode 739:每日温度 Daily Temperatures

    题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...

  7. Daily Temperatures

    Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...

  8. 69.Daily Temperatures(日常气温)

    Level:   Medium 题目描述: Given a list of daily temperatures T, return a list such that, for each day in ...

  9. 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...

随机推荐

  1. java利用webuploader实现超大文件分片上传、断点续传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  2. learning scala extractor object

    package com.aura.scala.day01 import scala.util.Random object extractorObject { def main(args: Array[ ...

  3. [USACO08FEB]酒店Hotel 线段树

    [USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...

  4. linux系列(十五):tail命令

    1.命令格式: tail[必要参数][选择参数][文件] 2.命令功能: 用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理.常用查看日志文件. 3.命令参数: -f 循环读取 -q 不显示 ...

  5. jmeter+ant执行 报错:Reference xslt.classpath not found 【采坑记录】

    问题: report: BUILD FAILED E:\jmeter\apache-jmeter-4.0\testcase\build.xml:29: The following error occu ...

  6. Linux中查看某个端口占用情况

    譬如在linux中排查某个端口是否被占用,可以通过如下命令进行排查,排查方法如下: 1: 排查 : 应用是否被人debug.  8787 为端口号 netstat -anp |grep 8787 图中 ...

  7. BeanFactory 简介以及它 和FactoryBean的区别

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  8. CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)

    You are given a function ff written in some basic language. The function accepts an integer value, w ...

  9. 通过AS提交AndroidLibrary到JCenter仓库

    注意事项: //版本需要一致,如下版本对应gradle-4.4-all.zip dependencies { classpath 'com.android.tools.build:gradle:3.1 ...

  10. [游戏开发]imgui介绍

    创建窗口 ImGui::Begin("Hello, world!"); ImGui::End(); 其中, ImGui::Begin("Hello, world!&quo ...