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

竟然暴力解法也可AC:

class Solution {
public int[] dailyTemperatures(int[] temperatures) {
if(temperatures == null || temperatures.length == 0){
return null;
}
int len = temperatures.length;
int[] res = new int[len];
for(int i=0; i<len; i++){
int count = 0;
int cur = temperatures[i];
for(int j = i+1; j<len; j++){
int fu = temperatures[j];
if(fu > cur){
count = j-i;
break;
}
}
res[i] = count;
}
return res;
}
}

  另一种使用stack的解法:

class Solution {
public int[] dailyTemperatures(int[] temperatures) {
if(temperatures == null || temperatures.length == 0){
return null;
}
int len = temperatures.length;
int[] res = new int[len];
Stack<Integer> stack = new Stack<>();
for(int i = 0; i<len; i++){
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
int temp = stack.pop();
res[temp] = i - temp;
}
stack.push(i); }
return res;
}
}

LeetCode - Daily Temperatures的更多相关文章

  1. [LeetCode] Daily Temperatures 日常温度

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

  2. [Leetcode 739]*还有几天会升温 Daily Temperatures

    [题目] Given a list of daily temperatures T, return a list such that, for each day in the input, tells ...

  3. LeetCode 739. Daily Temperatures

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

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

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

  5. 739. Daily Temperatures - LeetCode

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

  6. LeetCode 739:每日温度 Daily Temperatures

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

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

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

  8. Leetcode739 - Daily Temperatures

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

  9. Daily Temperatures

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

随机推荐

  1. node(1) npm是什么?node的异步概念

    NPM是随同的NodeJS一起安装的包管理工具 他可以做什么? 1.可以从NPM服务器下载别人的东西使用 2.可以把自己的东西传到NPM服务器,让别人下载使用 淘宝的镜像会快一点      cnpm ...

  2. Invalid MEX-file: caffe.mexa64 的解决方案

    http://blog.csdn.net/iamzhangzhuping/article/details/53105708

  3. PM2报错‘Spawning PM2 daemon with pm2_home...’的解决方案

    问题 在某次因为SRE升级域名问题,导致了Node服务器代码死循环了,产生的504(Gateway timeout)错误. 登录到机器上看,正在用pm2查问题的原因中,突然发现错误从504变成的502 ...

  4. springboot学习章节代码-spring高级话题

    1.Spring Aware(获取Spring容器的服务) hi, i am guodaxia! test.txt package com.zhen.highlights_spring4.ch3.aw ...

  5. Python基础学习(第4天)

    Python进阶 第1课:词典(dictionary) 1.词典可以存储多个元素,存储多个元素的对象称为容器(container) 第2课:文本文件的读取写入 1.打开一个文件,用对象来代表这个文件 ...

  6. 循环神经网络-极其详细的推导BPTT

    首先明确一下,本文需要对RNN有一定的了解,而且本文只针对标准的网络结构,旨在彻底搞清楚反向传播和BPTT. 反向传播形象描述 什么是反向传播?传播的是什么?传播的是误差,根据误差进行调整. 举个例子 ...

  7. Vim 文件配置

    cat ~/.vimrc syntax on set nu set encoding=utf-8 set ts=4 set fileencodings=ucs-bom,utf-8,cp936 set ...

  8. codeforce150A(简单的求质数问题)

    A. Win or Freeze time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. 扩展HtmlHelper类实现Mvc4分页

    1.扩展HtmlHelper类方法Pager public static HtmlString Pager(this HtmlHelper htmlHelper, int currentPage, i ...

  10. wait_activity

    wait_activity(self, activity, timeout, interval=1): android特有的 返回的True 或 False :Agrs: - activity - 需 ...