LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/
题目:
Given a list of daily temperatures, produce a list 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 temperatures = [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].
题解:
Stack<Integer> stk里存index, 若当前temperature 更高, 就一直pop出来index, 计算天数差值保存.
最后stk中剩余的index说明这些位置上没有更加暖和的天气, 存0.
Time Complexity: O(n). n = T.length.
Space: O(n).
AC Java:
class Solution {
public int[] dailyTemperatures(int[] T) {
if(T == null || T.length == 0){
return T;
}
int len = T.length;
int [] res = new int[len];
Stack<Integer> stk = new Stack<Integer>();
for(int i = len-1; i>=0; i--){
while(!stk.isEmpty() && T[i]>=T[stk.peek()]){
stk.pop();
}
res[i] = stk.isEmpty() ? 0 : stk.peek()-i;
stk.push(i);
}
return res;
}
}
LeetCode 739. Daily Temperatures的更多相关文章
- LeetCode 739. Daily Temperatures (每日温度)
题目标签:HashMap 题目给了我们一组温度,让我们找出 对于每一天,要等多少天,气温会变暖.返回一组等待的天数. 可以从最后一天的温度遍历起,从末端遍历到开头,对于每一天的温度,把它在T里面的in ...
- 739. Daily Temperatures - LeetCode
Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
- 739. Daily Temperatures
https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- [Leetcode 739]*还有几天会升温 Daily Temperatures
[题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
- [LeetCode] Daily Temperatures 日常温度
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- LeetCode - Daily Temperatures
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
随机推荐
- HDU 4681 STRING dp+暴力。
题意:不说了很好懂. 这题这么水= =...当时竟然没有勇气暴力搜一下.昨天(好吧前天.)比赛的时候胃疼,看到这题想了一个办法就是对每一个出现最短的C串前后连接然后对这个串求最长公共子序列.其实优化一 ...
- ansible入门二(Ansible常见模块介绍)
本节内容: ansible命令基础 常见模块举例 一.ansible命令基础 语法: ansible <host-pattern> [-f forks] [-m module_name] ...
- Quartz教程四:Trigger
原文链接 | 译文链接 | 翻译:nkcoder 本系列教程由quartz-2.2.x官方文档翻译.整理而来,希望给同样对quartz感兴趣的朋友一些参考和帮助,有任何不当或错误之处,欢迎指正:有兴趣 ...
- 快速切题 poj 1003 hangover 数学观察 难度:0
Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103896 Accepted: 50542 Descr ...
- New Concept English three(17)
27W/m 65 Verrazano, an Italian about whom little is known, sailed into New York Harbour in 1524 and ...
- c# html内容处理类
using System; using System.Text; using System.Text.RegularExpressions; using System.Net; using Syste ...
- 给SqlParameter参数指定或不指定:@变量标识符的区别是什么?
对于sql语句中一个@的参数@au_id,在sqlParameter中,可以用@au_id,也可以用au_id. 要是bt点的,用两个@,你就不能随便省略@了. 对于sql语句 : select * ...
- 关于$_SERVER['PHP_AUTH_USER']
http://www.cnblogs.com/thinksasa/p/3421379.html PHP 的 HTTP 认证机制仅在 PHP 以 Apache 模块方式运行时才有效,因此该功能不适用于 ...
- web.xml文件模板
Servlet 2.3 deployment descriptor 注:web.xml中提示错误The content of element type "web-app" mus ...
- UART介绍
https://baike.baidu.com/item/UART/4429746?fr=aladdin