题目标签:HashMap

  题目给了我们一组温度,让我们找出 对于每一天,要等多少天,气温会变暖。返回一组等待的天数。

  可以从最后一天的温度遍历起,从末端遍历到开头,对于每一天的温度,把它在T里面的index存入 temp[101] 保存。然后对于每一天的温度,从温度+1 到100 全部遍历后,找出变暖温度里离当天最近的那一天。存入answer。具体看code。

Java Solution:

Runtime: 8 ms, faster than 91.33%

Memory Usage: 42.7 MB, less than 93.43%

完成日期:04/02/2019

关键点:从结尾开始遍历到开头。

class Solution {
public int[] dailyTemperatures(int[] T) {
int[] answer = new int[T.length];
int[] temp = new int[101];
Arrays.fill(temp, Integer.MAX_VALUE); for(int i = T.length - 1; i >= 0; i--) // iterate from end to start
{
int warmer_index = Integer.MAX_VALUE; for(int t = T[i] + 1; t <= 100; t++) // for this day, find the closest warmer day
{
if(temp[t] < warmer_index)
warmer_index = temp[t];
} if(warmer_index < Integer.MAX_VALUE)
answer[i] = warmer_index - i; // store current day's temp into temp array
temp[T[i]] = i;
} return answer;
}
}

参考资料:https://leetcode.com/problems/daily-temperatures/discuss/?currentPage=1&orderBy=recent_activity&query=

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 739. Daily Temperatures (每日温度)的更多相关文章

  1. LeetCode 739. Daily Temperatures

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

  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. [LeetCode] Daily Temperatures 日常温度

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

  5. LeetCoded第739题题解--每日温度

    每日温度 请根据每日 气温 列表,重新生成一个列表.对应位置的输出为:要想观测到更高的气温,至少需要等待的天数.如果气温在这之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temper ...

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

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

  7. 739. Daily Temperatures

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

  8. 每日温度(LeetCode Medium难度算法题)题解

    LeetCode 题号739中等难度 每日温度 题目描述: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 ...

  9. LeetCode 739:每日温度 Daily Temperatures

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

随机推荐

  1. TCP/IP和UDP的比较

    TCP.UDP详解 1.传输层存在的必要性 由于网络层的分组传输是不可靠的,无法了解数据到达终点的时间,无法了解数据未达终点的状态.因此有必要增强网络层提供服务的服务质量. 2.引入传输层的原因 面向 ...

  2. 【译】x86程序员手册27-7.6任务链

    7.6 Task Linking 任务链 The back-link field of the TSS and the NT (nested task) bit of the flag word to ...

  3. (转) 淘淘商城系列——使用SolrJ查询索引库

    http://blog.csdn.net/yerenyuan_pku/article/details/72908538 我们有必要在工程中写查询索引库的代码前先进行必要的测试.我们先到Solr服务页面 ...

  4. MySQL学习笔记(十二)__连接查询(一)

    连接查询含义:又称多表查询,当查询的字段来自多个表时,就会用到连接查询 笛卡尔乘积现象:表1 有 m 行,表2 有 n 行,结果 = m*n 行发生原因:没有有效的连接条件如何避免:添加有效的连接条件 ...

  5. 弹性分布式数据集(RDD)

    spark围绕弹性分布式数据集(RDD)的概念展开的,RDD是一个可以并行操作的容错集合. 创建RDD的方法: 1.并行化集合(并行化驱动程序中现有的集合) 调用SparkContext的parall ...

  6. IOS上MediaPlayer framework实现视频播放

    播放电影文件: iOS sdk中可以使用MPMoviePlayerController来播放电影文件.但是在iOS设备上播放电影文件有严格的格式要求,只能播放下面两个格式的电影文件. • H.264 ...

  7. php第二十五节课

    详情删除 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  8. 第3章 从Flux到Redux

    第3章 从Flux到Redux 3.1 Flux 单向数据流,React是用来替换Jquery的,Flux是以替换Backbone.js.Ember.js等MVC框架为主的. actionTypes. ...

  9. axios在vue项目中的一种封装方法

    记录下之前领导封装的axios请求 npm install axios // 安装 单独写个文件配置axios,此处为request.js import axios from 'axios' //自定 ...

  10. 15Ajax、JSON

    15Ajax.JSON-2018/07/27 1. ThreadLocal 总结:调用该类的get方法,永远返回当前线程放入的数据.线程局部变量. 保证线程安全 (第二阶段day14后半部分视频以及1 ...