LeetCode 739. Daily Temperatures (每日温度)
2024-08-30 21:44:17
题目标签: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/
最新文章
- JS 初级 二(接上)
- 你真的了解iOS代理设计模式吗?
- 【BZOJ】3991: [SDOI2015]寻宝游戏
- Oracle中“行转列”的实现方式
- 静态函数(面向过程的static关键字)
- Linux下yum升级安装PHP 5.5
- JavaScript密码复杂度
- UVa 101 - The Blocks Problem(积木问题,指令操作)
- mysql:键缓存
- hdu 5476 Explore Track of Point(2015上海网络赛)
- 转: 从Mysql某一表中随机读取n条数据的SQL查询语句
- 【并发编程】Executor类的继承结构
- swoole 异步队列
- 劳动节BT5 aircrack-ng战记
- ccf集合竞价
- 基于百度AI开放平台的人脸识别及语音合成
- [模板] 回文树/回文自动机 &;&; BZOJ3676:[Apio2014]回文串
- XXXXX,这个域名
- 在Jenkins上配置批处理删除远程共享目录7天以上的文件
- ESXI6时间源快速同步
热门文章
- Android 读取asset文件
- sqlalchemy.exc.InvalidRequestError: Entity &#39;<;class &#39;model.TestCase&#39;>;&#39; has no property &#39;project&#39;
- Java8(一)--lambda表达式
- ThinkPHP---ue编辑器
- Spring Boot 创建hello world项目
- 用sed写配置IP脚本参数
- document.write() 和 document.writeln区别
- 【ssm】spring功能讲解
- document.documentElement
- Python,socket编程