Skip to content

Commit 3fbf1da

Browse files
committed
ok
1 parent 1549e40 commit 3fbf1da

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/com/leetcode/Main739.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* 739. 每日温度
8+
* 请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。
9+
*
10+
* 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。
11+
*
12+
* 提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。
13+
*/
14+
public class Main739 {
15+
public int[] dailyTemperatures(int[] T) {
16+
if (T == null || T.length == 0)
17+
return new int[0];
18+
LinkedList<Integer> stack = new LinkedList<>();
19+
int[] res = new int[T.length];
20+
for (int i = 0; i < T.length; i++) {
21+
if (stack.isEmpty()) {
22+
stack.offerLast(i);
23+
} else {
24+
while (!stack.isEmpty() && T[i] > T[stack.peekLast()]) {
25+
int preIndex = stack.pollLast();
26+
res[preIndex] = i - preIndex;
27+
}
28+
stack.offerLast(i);
29+
}
30+
}
31+
return res;
32+
}
33+
34+
public static void main(String[] args) {
35+
Main739 m = new Main739();
36+
int[] T = {73, 74, 75, 71, 69, 72, 76, 73};
37+
System.out.println(Arrays.toString(m.dailyTemperatures(T)));
38+
}
39+
}

0 commit comments

Comments
 (0)