剑指 Offer II 038:每日温度
This commit is contained in:
parent
817ecc1515
commit
a74f90f00a
68
src/main/java/leetcode/editor/cn/IIQa4I.java
Normal file
68
src/main/java/leetcode/editor/cn/IIQa4I.java
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
//<p>请根据每日 <code>气温</code> 列表 <code>temperatures</code> ,重新生成一个列表,要求其对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 <code>0</code> 来代替。</p>
|
||||||
|
//
|
||||||
|
//<p> </p>
|
||||||
|
//
|
||||||
|
//<p><strong>示例 1:</strong></p>
|
||||||
|
//
|
||||||
|
//<pre>
|
||||||
|
//<strong>输入:</strong> <span><code>temperatures</code></span> = [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>
|
||||||
|
//
|
||||||
|
//<p> </p>
|
||||||
|
//
|
||||||
|
//<p>
|
||||||
|
// <meta charset="UTF-8" />注意:本题与主站 739 题相同: <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)
|
||||||
|
|
||||||
|
}
|
39
src/main/java/leetcode/editor/cn/doc/content/IIQa4I.md
Normal file
39
src/main/java/leetcode/editor/cn/doc/content/IIQa4I.md
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<p>请根据每日 <code>气温</code> 列表 <code>temperatures</code> ,重新生成一个列表,要求其对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 <code>0</code> 来代替。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong> <span><code>temperatures</code></span> = [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>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<meta charset="UTF-8" />注意:本题与主站 739 题相同: <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>
|
Loading…
Reference in New Issue
Block a user