题目标签: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. struts2之通配符映射

    系统有n多个请求时候,不可能以一个action对应一个映射.可以用通配符映射将成百上千请求简化成一个通用映射. 通配符映射规则:1.若找到多个匹配,没有通配符的将胜出. 2.若指定的动作不存在,str ...

  2. 【转载】testlink 1.8.5 安装错误的解决方法

    TestLink所需环境为PHP+MYSQL (支持MS SQL等),系统推荐使用PHP5.2,安装成功以后,如果运行时出错,主要两种错: [1].HP Warning: strtotime(): I ...

  3. Queries for Number of Palindromes(求任意子列的回文数)

    H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...

  4. tf idf公式及sklearn中TfidfVectorizer

    在文本挖掘预处理之向量化与Hash Trick中我们讲到在文本挖掘的预处理中,向量化之后一般都伴随着TF-IDF的处理,那么什么是TF-IDF,为什么一般我们要加这一步预处理呢?这里就对TF-IDF的 ...

  5. C++ std::map的安全遍历并删除元素的方法

    首先我们讲遍历std::map, 大部分人都能写出第一种遍历的方法,但这种遍历删除的方式并不太安全. 第一种 for循环变量: #include<map> #include<stri ...

  6. Python之实例属性和类属性

    参考原文 廖雪峰Python 实例属性和类属性 在前面已经说过由于Python是动态语言,可以根据类的实例绑定任何的属性. 给实例绑定属性的方法是通过实例变量,或者self变量绑定的: class S ...

  7. java计算两地距离(公里)

    //目标经度,目标纬度,自己经度,自己纬度 public static double getDistance(double lon1, double lat1, double lon2, double ...

  8. 洛谷——P2261 [CQOI2007]余数求和

    P2261 [CQOI2007]余数求和 关键在于化简公式,题目所求$\sum_{i=1}^{n}k\mod i$ 简化式子,也就是$\sum_{i=1}^{n}(k-\frac{k}{i}\time ...

  9. 【nginx】记录nginx+php-fpm实现大文件下载排坑的过程

    先上一段代码,支持大文件下载和断点续传,代码来源互联网. set_time_limit(0); // 省略取文件路径的过程,这里直接是文件完整路径 $filePath = get_save_path( ...

  10. mac下用crontab实现pytho3脚本自动定期执行,包括scrapy的定期执行

    呃 其实要明天上午才能知道是否成功,毕竟改了一个小参数的. 首先,来学两个小命令: step1: $ sudo crontab -e step2: # 然后提示password输入密码,即可进入编辑页 ...