【题目】

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].

还有几天会升温。

https://leetcode.com/problems/daily-temperatures

【思路】

暴力求解,用时太长了……参考答案用stack

【代码】

堆栈,倒序比较,当

1、stack不为空,T[i-1]>T[i],证明不会升温,pop出栈跳出。进行步骤2

2、判断stack是否为空,空栈无条件满足返回0,否则【顶点(升温点)-循环数i 】为天数

3、把i压入栈。

class Solution {
public int[] dailyTemperatures(int[] T) {
int[] ans = new int[T.length];
Stack<Integer> stack = new Stack();
for (int i = T.length - 1; i >= 0; --i) {
while (!stack.isEmpty() && T[i] >= T[stack.peek()])
stack.pop();
ans[i] = stack.isEmpty() ? 0 : stack.peek() - i;
stack.push(i);
}
return ans;
}
}

方便理解

class Solution {
public int[] dailyTemperatures(int[] T) {
int tmp[]=new int[T.length];
Arrays.fill(tmp, 1);
tmp[T.length-1]=0;
for(int i=0;i<T.length-1;i++){
int j=i+1;
while(T[j]<=T[i]){
++j;
tmp[i]++;
if(j>=T.length){
break;
}
}
tmp[i]=j<T.length?tmp[i]:0;
}
return tmp;
}
}

[Leetcode 739]*还有几天会升温 Daily Temperatures的更多相关文章

  1. 739. Daily Temperatures - LeetCode

    Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...

  2. LeetCode 739. Daily Temperatures

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

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

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

  4. LeetCode 739:每日温度 Daily Temperatures

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

  5. [LeetCode] Daily Temperatures 日常温度

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

  6. LeetCode - Daily Temperatures

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

  7. Leetcode739 - Daily Temperatures

    题目描述 Leetcode 739 本题考察了栈的使用.题目输入是一段温度值列表,然后返回一个列表.这个列表包含了输入列表中每一天还有多少天温度升高.如果未来没有升高的情况,则输入 0. # Exam ...

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

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

  9. Daily Temperatures

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

随机推荐

  1. Axure 第一次接触动态面板

    动态面板可以理解为有多维空间的一个容器,里面可以设置多个state,每个state里面可以放多个元件 先把对应的元件设置为动态面板并且隐藏: 本段教程主要包括三种应用场景: 1.邮箱不为空.密码为空时 ...

  2. Android+Struts2实现简单的前后台交互--Android网络编程

    1.下面测试截图,基本过程就是:点击按钮向服务器端发送请求,后台收到请求后给出返回的数据,前台只需要显示服务端数据就可以了.例子很简单能但引发很多思考,博主学了杂七杂八的,这又在来想杂学Android ...

  3. Vue.js,select中的option用ajax想循环控制值的显示 v-model可以实现但提示报错,这是为什么?

    应该将v-model换成:value,因为v-model只能绑定一个值,无法绑定多个值 <select v-model="citys">       <optio ...

  4. Tensor RT使用记录

    Tensor RT的介绍在此不做赘述. 自己在服务器上本打算装Tensor RT来着,不过过程很艰辛,最后发现服务器的cudnn版本偏低了,还需要升级cudnn的版本.故,在自己的电脑上了装了下Ten ...

  5. git clone项目

    1. 生成公钥和私钥 ssh-keygen 2. 将公钥添加到github或者gitlab上,一般github或者gitlab允许添加多个公钥,可能是考虑到用户使用不同的机器了吧,还是很贴心的. 3. ...

  6. .net 基础

    之前给大家总结了java的面试几次技巧总结,同学们看了觉得还是不错,能够得到大家的认可,感觉还是挺不错的.现在又有同学来想小编索要.NET面试的总结了,好吧.谁让小编这么好呢!以下是.NET面试之框架 ...

  7. Leetcode 993. 二叉树的堂兄弟节点

    993. 二叉树的堂兄弟节点  显示英文描述 我的提交返回竞赛   用户通过次数195 用户尝试次数229 通过次数195 提交次数462 题目难度Easy 在二叉树中,根节点位于深度 0 处,每个深 ...

  8. QString 的用法

    C++语言提供了两种字符串的实现:C风格的字符串,以'\0‘结尾;std::string,即标准模版库中的类.Qt则提供了自己的字符串实现:QString,QString不用担心内存分配以及关于'\0 ...

  9. Scheduler & Task & Worker & Thread & Request & Session & Connection of SQL Server

    MSSQL一直以来被人们认为简单.好学,但等到大家掌握了入门操作,深入理解起来又觉得非常的“拧巴”,尤其是对用惯了Oracle的同学来说,究其根本原因,无非是MSSQL引入和暴露了太多的概念.细节和理 ...

  10. Qt Widgets——动作类与小部件菜单项

    本文主要涉及以下三个类: QAction ——QWidgetAction QActionGroup QAction可称为动作类,它一般可当作菜单中的项组成菜单,也可作为工具栏上的按钮,它主要由图标.文 ...