[Leetcode 739]*还有几天会升温 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.
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的更多相关文章
- 739. Daily Temperatures - LeetCode
Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
- 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 ...
- Leetcode739 - Daily Temperatures
题目描述 Leetcode 739 本题考察了栈的使用.题目输入是一段温度值列表,然后返回一个列表.这个列表包含了输入列表中每一天还有多少天温度升高.如果未来没有升高的情况,则输入 0. # Exam ...
- [Swift]LeetCode739. 每日温度 | Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
- Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...
随机推荐
- (9)进程---JoinableQueue队列
消费者模型-->存和取得过程 和Queue队列区别:解决了Queue队列拿取完,程序阻塞不能自动关闭(依靠放入None来解决)的问题--->参见上个例子 put 存入, get 获取 q. ...
- 雷林鹏分享:XML - E4X
XML - E4X E4X 向 JavaScript 添加了对 XML 的直接支持. E4X 实例 var employees= Tove 32 Jani 26 ; document.write(em ...
- English trip M1 - AC9 Nosey people 爱管闲事的人 Teacher:Solo
In this lesson you will learn to talk about what happened. 在本课中,您将学习如何谈论发生的事情. 课上内容(Lesson) # four “ ...
- 编译spark-0.9.1
准备工作:注意 spark-0.9.1 要求 scala-2.10.x 版本,sbt-0.12.4版本. centos 6.4 x64 系统,java 1.7.0 x64 1,安装 scala-2.1 ...
- fedora21 中lamp的搭建(测试没有问题)
LAMP Stands for Linux,Apache,MySQL and PHP. Most of the websites works with the above combination. T ...
- Vmware安装Kali
下载软件 破解版的Vmware14 kali(我的是kali-linux-2018.2-amd64) 配置虚拟机 新建虚拟机,选择自定义 虚拟机硬件兼容性 选择虚拟机硬件兼容性为Workstation ...
- apiCloud 三方分享,微信好友分享,微信朋友圈分享,QQ分享,微博分享
首先查看我的这篇有关三方登录的博客,地址是http://www.cnblogs.com/gqx-html/p/8303567.html,配置完三方数据后可以从上一篇文章中的链接跳转到各个登录查看api ...
- Vue音乐项目笔记(一)
看到一位小可爱的手记,这里记录一下自己需要注意的地方的链接 1.手写轮播图(上) https://blog.csdn.net/weixin_40814356/article/details/80298 ...
- Laravel中APP_KEY起什么作用
框架中是这样描述的: This key is used by the Illuminate encrypter service and should be set to a random, 32 ch ...
- SQL server语句中如何实现分页
SELECT TOP 页大小 *FROM table1WHERE id NOT IN ( SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id )ORDER ...