LeetCode 739. Daily Temperatures (每日温度)
题目标签: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;
}
}
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 739. Daily Temperatures (每日温度)的更多相关文章
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- 739. Daily Temperatures - LeetCode
Question 739. Daily Temperatures Solution 题目大意:比今天温度还要高还需要几天 思路:笨方法实现,每次遍历未来几天,比今天温度高,就坐标减 Java实现: p ...
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
- [LeetCode] Daily Temperatures 日常温度
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how ma ...
- LeetCoded第739题题解--每日温度
每日温度 请根据每日 气温 列表,重新生成一个列表.对应位置的输出为:要想观测到更高的气温,至少需要等待的天数.如果气温在这之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temper ...
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- 739. Daily Temperatures
https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...
- 每日温度(LeetCode Medium难度算法题)题解
LeetCode 题号739中等难度 每日温度 题目描述: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 ...
- LeetCode 739:每日温度 Daily Temperatures
题目: 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperature ...
随机推荐
- Mantis 配置与使用学习
转载自:http://blog.csdn.net/xifeijian/article/category/1429687
- JBoss4.2的启动方式-Jboss无法通过IP地址访问,只能用localhost访问
JBOSS版本:4.2.3GA症状:服务器无法通过IP地址去访问,只能用127.0.0.1或者localhost来访问. 开始怀疑是端口没有放开,用telnet ip 80 也不能连接,就一直怀疑端口 ...
- JAVA环境变量配置后未变动配置失效处理
环境: Windows 7 x64 配置方案来源于教程: http://www.mamicode.com/info-detail-563355.html 配置方案出现的问题: 正确配置JAVA环境变量 ...
- parsley.js验证的基本引用
前段时间看到博客有些parsley.js验证,只是对parsley.js验证框架基本的应用,对parsley.js更深层理解没有介绍和demo 比如:异步请求,扩展验证的写法,我把我学到的parsle ...
- codeforces_300C_组合数_快速幂
C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【Linux】CentOS安装Jenkins
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo sudo rpm -- ...
- 并发编程学习笔记(5)----AbstractQueuedSynchronizer(AQS)原理及使用
(一)什么是AQS? 阅读java文档可以知道,AbstractQueuedSynchronizer是实现依赖于先进先出 (FIFO) 等待队列的阻塞锁和相关同步器(信号量.事件,等等)提供一个框架, ...
- 用 webpack 实现持久化缓存
什么是持久化缓存? 原文链接https://sebastianblade.com/using-webpack-to-achieve-long-term-cache/ 缓存(cache)一直是前端性能优 ...
- Java面试问题——线程全面详解总结
一.多线程是什么?为什么要用多线程? 介绍多线程之前要介绍线程,介绍线程则离不开进程. 首先进程 :是一个正在执行中的程序,每一个进程执行都有一个执行顺序,该顺序是一个执行路径,或者叫一个控制单元: ...
- CAD由一个自定义实体事件中的id得到自定义实体对象(com接口VB语言)
由一个自定义实体事件中的id得到自定义实体对象.该函数只能在自定义实体事件中调用. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...