2022/1/7
2022/1/7 6:06:08
本文主要是介绍2022/1/7,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
力扣739 每日温度
请根据每日 气温 列表 temperatures ,请计算在每一天需要等几天才会有更高的温度。如果气温在这之后都不会升高,请在该位置用 0 来代替。
示例一 输入: temperatures = [73,74,75,71,69,72,76,73] 输出: [1,1,4,2,1,1,0,0] 示例二 输入: temperatures = [30,40,50,60] 输出: [1,1,1,0] 示例三 输入: temperatures = [30,60,90] 输出: [1,1,0]
思路
- 暴力for、for
- 单调栈
维护一个有序的栈,当第i个数据比栈顶数据大时,一直弹出栈顶元素,更新下标差(所以,栈存的是下表);将第i个元素入栈。
class Solution { public int[] dailyTemperatures(int[] temperatures) { int len = temperatures.length; int next[] = new int[len]; Stack<Integer> stk = new Stack<Integer>(); for (int i=0; i<len; i++){ while(!stk.empty() && temperatures[i]>temperatures[stk.peek()]){ int idx = stk.pop(); next[idx] = i-idx; } stk.push(i); } return next; } }
这篇关于2022/1/7的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求