剑指 Offer II 038:每日温度

This commit is contained in:
轩辕龙儿 2023-02-07 13:54:31 +08:00
parent 817ecc1515
commit a74f90f00a
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,68 @@
//<p>请根据每日 <code>气温</code> 列表 <code>temperatures</code>&nbsp;重新生成一个列表要求其对应位置的输出为要想观测到更高的气温至少需要等待的天数如果气温在这之后都不会升高请在该位置用&nbsp;<code>0</code> 来代替</p>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1:</strong></p>
//
//<pre>
//<strong>输入:</strong> <span><code>temperatures</code></span> = [73,74,75,71,69,72,76,73]
//<strong>输出:</strong>&nbsp;[1,1,4,2,1,1,0,0]
//</pre>
//
//<p><strong>示例 2:</strong></p>
//
//<pre>
//<strong>输入:</strong> temperatures = [30,40,50,60]
//<strong>输出:</strong>&nbsp;[1,1,1,0]
//</pre>
//
//<p><strong>示例 3:</strong></p>
//
//<pre>
//<strong>输入:</strong> temperatures = [30,60,90]
//<strong>输出: </strong>[1,1,0]</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示</strong></p>
//
//<ul>
// <li><code>1 &lt;=&nbsp;temperatures.length &lt;= 10<sup>5</sup></code></li>
// <li><code>30 &lt;=&nbsp;temperatures[i]&nbsp;&lt;= 100</code></li>
//</ul>
//
//<p>&nbsp;</p>
//
//<p>
// <meta charset="UTF-8" />注意本题与主站 739&nbsp;题相同&nbsp;<a href="https://leetcode-cn.com/problems/daily-temperatures/">https://leetcode-cn.com/problems/daily-temperatures/</a></p>
//
//<div><div>Related Topics</div><div><li></li><li>数组</li><li>单调栈</li></div></div><br><div><li>👍 73</li><li>👎 0</li></div>
package leetcode.editor.cn;
// 剑指 Offer II 038:每日温度
public class IIQa4I {
public static void main(String[] args) {
Solution solution = new IIQa4I().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int[] res = new int[temperatures.length];
int index = temperatures.length - 1;
res[index] = 0;
for (int i = temperatures.length - 2; i >= 0; i--) {
if (temperatures[i] < temperatures[index]) {
res[i] = index - i;
} else {
index = i;
res[i] = 0;
}
}
return res;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,39 @@
<p>请根据每日 <code>气温</code> 列表 <code>temperatures</code>&nbsp;,重新生成一个列表,要求其对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用&nbsp;<code>0</code> 来代替。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> <span><code>temperatures</code></span> = [73,74,75,71,69,72,76,73]
<strong>输出:</strong>&nbsp;[1,1,4,2,1,1,0,0]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> temperatures = [30,40,50,60]
<strong>输出:</strong>&nbsp;[1,1,1,0]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> temperatures = [30,60,90]
<strong>输出: </strong>[1,1,0]</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;=&nbsp;temperatures.length &lt;= 10<sup>5</sup></code></li>
<li><code>30 &lt;=&nbsp;temperatures[i]&nbsp;&lt;= 100</code></li>
</ul>
<p>&nbsp;</p>
<p>
<meta charset="UTF-8" />注意:本题与主站 739&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/daily-temperatures/">https://leetcode-cn.com/problems/daily-temperatures/</a></p>
<div><div>Related Topics</div><div><li></li><li>数组</li><li>单调栈</li></div></div><br><div><li>👍 73</li><li>👎 0</li></div>