原题链接在这里: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;
}
}

类似Next Greater Element I.

LeetCode 739. Daily Temperatures的更多相关文章

  1. LeetCode 739. Daily Temperatures (每日温度)

    题目标签:HashMap 题目给了我们一组温度,让我们找出 对于每一天,要等多少天,气温会变暖.返回一组等待的天数. 可以从最后一天的温度遍历起,从末端遍历到开头,对于每一天的温度,把它在T里面的in ...

  2. 739. Daily Temperatures - LeetCode

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

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

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

  4. 739. Daily Temperatures

    https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...

  5. 739. Daily Temperatures && 单调栈 && Python collections deque

    题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...

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

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

  7. LeetCode 739:每日温度 Daily Temperatures

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

  8. [LeetCode] Daily Temperatures 日常温度

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

  9. LeetCode - Daily Temperatures

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

随机推荐

  1. Python 基础教程(有经典的例子)

    http://www.runoob.com/python/os-listdir.html

  2. IOS-实战分享:实时美颜滤镜是怎样炼成的

    作者:琨君 原文链接:http://www.jianshu.com/p/945fc806a9b4 本文获作者授权转载 背景 前段时间由于项目需求,做了一个基于GPUImage的实时美颜滤镜.现在各种各 ...

  3. linux---网络相关配置,ssh服务,bash命令及优先级,元字符

    - 二:临时配置网络(ip,网关,dns)+永久配置 临时配置: [root@nfs-server ~]# ifconfig ens32: flags=4163<UP,BROADCAST,RUN ...

  4. angularJS---service

    service ng的服务是这样定义的: Angular services are singletons objects or functions that carry out specific ta ...

  5. jersey实现跨服务器上传

    1.导入跨服务器上传文件jar文件 <dependency> <groupId>commons-io</groupId> <artifactId>com ...

  6. js获取显示器、页面等高度 (转)

    网页可见区域宽:document.body.clientWidth网页可见区域高:document.body.clientHeight网页可见区域宽:document.body.offsetWidth ...

  7. python爬取商品信息

    老严要爬某网购网站的商品信息,正好我最近在学python,就一起写了一个简单的爬虫程序. 需求:某网的商品信息,包括商品名,市场价和售价 工具:python2.7.8,urllib2,re #codi ...

  8. LeetCode OJ:Bulls and Cows (公牛与母牛)

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  9. C++中几个值得分析的小问题(2)

    下面有3个小问题,作为C++ Beginner你一定要知道错在哪里了. 1.派生类到基类的引用或指针转换一定“完美”存在? 一般情况,你很可能会认为:派生类对象的引用或指针转换为基类对象的引用或指针是 ...

  10. win7下pyhton3.6创建django2的pycharm项目

    1.进入python虚拟环境: workon workon oneenv 2. 在虚拟环境中安装django,也可以使用pycharm上的自动安装,但那个比较慢,所有还是在cmd中安装的好 pip i ...