[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 ...
随机推荐
- Getting started with Processing 第九章总结
函数 函数的力量在于模块化. 范例 掷骰子 rollDice()函数 rollDice(int numSides){int d = 1+int(random(numSides));println(&q ...
- 国外(英文)——WPF较好的奇葩问题解决网站
https://stackoverflow.com/questions/6415908/c-sharp-wpf-datagrid-vertical-scroll
- docker 基本操作
# 常用命令 docker run 镜像 docker images 查看所有镜像 docke ps 查看运行中的容器 docker ps -a 列出所有容器 docker st ...
- bzoj2555: SubString sam+lct
题意:懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. 题解 ...
- js异常处理
<script> function myFunction() { try { var x=document.getElementById("demo").value; ...
- oracle having sum group by 详解
Aggregate functions (like SUM) often need an added GROUP BY functionality. 集合函数(类似SUM)经常需要用GROUP BY来 ...
- SQL - 数据查询
数据查询是数据库的核心操作.SQL 提供了 select 语句进行数据查询,该语句的一般格式为: select [ ALL | distinct ] <目标列表达式> [ ,<目 ...
- 网络编程socketserver实现并发
import socketserver import struct import json import os class FtpServer(socketserver.BaseRequestHand ...
- [洛谷 P2508] 圆上的整点
题目描述 求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. 输入输出格式 输入格式: r 输出格式: 整点个数 输入输出样例 输入样例#1: 4 输出样例#1: 4 说明 n ...
- [codechef July Challenge 2017] Chef and Sign Sequences
CHEFSIGN: 大厨与符号序列题目描述大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串.记字符串长度为 N,大厨想要在字符串的开头.结尾,和 ...