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 ...
随机推荐
- EasyUI datagrid 多条件查询
<script type="text/javascript"> $(function () { $("#dg").datagrid({ url: ' ...
- eclipse使用lombok
1.下载lombok.jar,将lombok复制到eclipse的安装路径下,如图: 2.在eclipse.ini配置文件最后加入:-javaagent:D:\Program Files\Eclips ...
- ZOJ 2599 Graduated Lexicographical Ordering ★(数位DP)
题意 定义两个数的比较方法,各位数字之和大的数大,如果数字和相等则按字典序比较两个数的大小.输入n,k,求:1.数字k的排名:2.排名为k的数. 思路 算是一类经典的统计问题的拓展吧~ 先来看第一问. ...
- 安装VMware Tools:Ubuntu
1.首先准备好linux.iso,在安装目录下应该可以找到,我使用的是这个: 链接:http://pan.baidu.com/s/1nuGQyIt 密码:b5mn 2.打开Ubuntu,CD中加载该i ...
- Spring入门4.AOP配置深入
Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...
- C# 图片缩放,拖拽后保存成图片的功能
窗体界面部分如下: 鼠标的缩放功能需要手动在 OpertaionImg.Designer.cs 文件里面添加一句代码,具体代码如下: //picturePhoto显示图片的控件 this.pictur ...
- 配置django上传文件目录的http访问
1.假设你的html模版文件使用以下路径来访问上传文件内容: url="{{ MEDIA_URL }}{{ images.image }}" 2.设置settings文件: MED ...
- Week12《java程序设计》第12次作业总结
Week12<java程序设计>第12次作业总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 ...
- node 垃圾回收
一些思考 回收 nodejs垃圾回收 跟浏览器js不同, 以下代码会找出内存泄露 var theThing = null var replaceThing = function () { var o ...
- git clone 提示输入git@xxx的密码
如下: suse:~/ecox # git clone git@vcs.in.ww-it.cn:ecox/ecox.git 正克隆到 'ecox'... git@vcs.in.ww-it.cn's p ...