题目标签: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/

最新文章

  1. JS 初级 二(接上)
  2. 你真的了解iOS代理设计模式吗?
  3. 【BZOJ】3991: [SDOI2015]寻宝游戏
  4. Oracle中“行转列”的实现方式
  5. 静态函数(面向过程的static关键字)
  6. Linux下yum升级安装PHP 5.5
  7. JavaScript密码复杂度
  8. UVa 101 - The Blocks Problem(积木问题,指令操作)
  9. mysql:键缓存
  10. hdu 5476 Explore Track of Point(2015上海网络赛)
  11. 转: 从Mysql某一表中随机读取n条数据的SQL查询语句
  12. 【并发编程】Executor类的继承结构
  13. swoole 异步队列
  14. 劳动节BT5 aircrack-ng战记
  15. ccf集合竞价
  16. 基于百度AI开放平台的人脸识别及语音合成
  17. [模板] 回文树/回文自动机 &amp;&amp; BZOJ3676:[Apio2014]回文串
  18. XXXXX,这个域名
  19. 在Jenkins上配置批处理删除远程共享目录7天以上的文件
  20. ESXI6时间源快速同步

热门文章

  1. Android 读取asset文件
  2. sqlalchemy.exc.InvalidRequestError: Entity &#39;&lt;class &#39;model.TestCase&#39;&gt;&#39; has no property &#39;project&#39;
  3. Java8(一)--lambda表达式
  4. ThinkPHP---ue编辑器
  5. Spring Boot 创建hello world项目
  6. 用sed写配置IP脚本参数
  7. document.write() 和 document.writeln区别
  8. 【ssm】spring功能讲解
  9. document.documentElement
  10. Python,socket编程