-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution739.java
More file actions
executable file
·32 lines (29 loc) · 1006 Bytes
/
Copy pathSolution739.java
File metadata and controls
executable file
·32 lines (29 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
class Solution739 {
public int[] dailyTemperatures(int[] temperatures) {
// 单调栈
Stack<Integer> s = new Stack<>();
int[] res = new int[temperatures.length];
for (int i = temperatures.length - 1; i >= 0; i--) {
while (!s.isEmpty() && temperatures[s.peek()] <= temperatures[i])
s.pop();
// 栈中只剩下比自己大的元素
res[i] = s.isEmpty() ? 0 : s.peek() - i;
s.push(i); // 将下标压入栈
}
return res;
}
public int[] dailyTemperature(int[] temperatures) {
// 单调栈
Stack<Integer> s = new Stack<>();
int[] res = new int[temperatures.length];
for (int i = 0; i < temperatures.length; i++) {
while (!s.isEmpty() && temperatures[s.peek()] < temperatures[i]) {
int r = s.pop();
res[r] = i - r;
}
s.push(i);
}
return res;
}
}