博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
739. Daily Temperatures(python+cpp)
阅读量:3703 次
发布时间:2019-05-21

本文共 1181 字,大约阅读时间需要 3 分钟。

题目:

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.

For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [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].

解释:

用栈做,维护一个降序的栈,栈中存储的是indexindex本身不是降序的,但是index对应的T[index]是降序的,当出现比栈中一些index对应的T[index]大的T[i]的时候,把那些index出栈,并保存好位置差,然后把当前的i压入栈,这样就可以实现要求。
python代码:

class Solution:    def dailyTemperatures(self, T):        """        :type T: List[int]        :rtype: List[int]        """        ans=[0]*len(T)        stack=[]        for i in range(len(T)):            while stack and T[stack[-1]]

c++代码:

#include 
using namespace std;class Solution {
public: vector
dailyTemperatures(vector
& T) {
stack
_stack; int n=T.size(); vector
result(n,0); for(int i=0;i

总结:

while的时候可以多加一些判断。

转载地址:http://oglcn.baihongyu.com/

你可能感兴趣的文章
HTML表格和HTML表单
查看>>
JSP访问数据库,Session对象和九大内置对象
查看>>
Springboot分层图解
查看>>
并查集(Disjiont Set)
查看>>
Java操作HBase
查看>>
Linux编程考前测试题
查看>>
Openstack面试题和知识点总结
查看>>
C++ 实例化一个对象
查看>>
基于Spring boot+Vue的在线考试系统
查看>>
大数据学习路线
查看>>
前端学习路线
查看>>
推荐几个单机游戏下载网、高质量图片下载网
查看>>
数据库查询
查看>>
单臂路由配置
查看>>
静态路由及动态路由 RIP配置
查看>>
现代密码学:AES
查看>>
现代密码学:密码协议
查看>>
现代密码学:密钥管理
查看>>
数据库增删改
查看>>
RSA公钥
查看>>