leetcode单调栈-每日温度
import java.util.Stack;
/**
<p>给定一个整数数组 <code>temperatures</code> ,表示每天的温度,返回一个数组 <code>answer</code> ,其中 <code>answer[i]</code> 是指在第 <code>i</code> 天之后,<span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">才会有更高的温度</font></span></span></span></span>。如果气温在这之后都不会升高,请在该位置用 <code>0</code> 来代替。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> <code>temperatures</code> = [73,74,75,71,69,72,76,73]
<strong>输出:</strong> [1,1,4,2,1,1,0,0]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> temperatures = [30,40,50,60]
<strong>输出:</strong> [1,1,1,0]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> temperatures = [30,60,90]
<strong>输出: </strong>[1,1,0]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= temperatures.length <= 10<sup>5</sup></code></li>
<li><code>30 <= temperatures[i] <= 100</code></li>
</ul>
<div><div>Related Topics</div><div><li>栈</li><li>数组</li><li>单调栈</li></div></div><br><div><li>👍 1122</li><li>👎 0</li></div>
*/
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
//单调栈,只是换成坐标进行计算,注意判断那块 小于等于都要去掉
public int[] dailyTemperatures(int[] temperatures) {
int[] res = new int[temperatures.length];
Stack<Integer> s = new Stack<>();
for (int i = temperatures.length-1 ; i >= 0 ; i--) {
while(!s.isEmpty() && temperatures[s.peek()]<=temperatures[i]){
s.pop();
}
res[i] = (s.isEmpty() || temperatures[s.peek()]<=temperatures[i])?0:s.peek()-i;
s.push(i);
}
return res;
}
}
//leetcode submit region end(Prohibit modification and deletion)
不恋尘世浮华,不写红尘纷扰
标签:
leetcode刷题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2021-04-25 Lc216_组合总和III
2021-04-25 下载yum并解压