-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode739.java
More file actions
51 lines (45 loc) · 1.75 KB
/
LeetCode739.java
File metadata and controls
51 lines (45 loc) · 1.75 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
public class LeetCode739 {
public static void main(String[] args) {
// 输入: temperatures = [73,74,75,71,69,72,76,73]
// 输出: [1,1,4,2,1,1,0,0]
System.out.println(
Arrays.toString(new Solution739().dailyTemperatures(new int[] { 73, 74, 75, 71, 69, 72, 76, 73 })));
// 输入: temperatures = [30,40,50,60]
// 输出: [1,1,1,0]
System.out.println(
Arrays.toString(new Solution739().dailyTemperatures(new int[] { 30, 40, 50, 60 })));
// 输入: temperatures = [30,60,90]
// 输出: [1,1,0]
System.out.println(
Arrays.toString(new Solution739().dailyTemperatures(new int[] { 30, 60, 90 })));
// 输入: temperatures = [99,99,99,100]
// 输出: [3,2,1,0]
System.out.println(
Arrays.toString(new Solution739().dailyTemperatures(new int[] { 99, 99, 99, 100 })));
// 输入: temperatures = [34,80,80,34,34,80,80,80,80,34]
// 输出: [1,0,0,2,1,0,0,0,0,0]
System.out.println(
Arrays.toString(
new Solution739().dailyTemperatures(new int[] { 34, 80, 80, 34, 34, 80, 80, 80, 80, 34 })));
}
}
/**
* 递减栈
*/
class Solution739 {
public int[] dailyTemperatures(int[] temperatures) {
Deque<Integer> stack = new LinkedList<>();
int[] res = new int[temperatures.length];
for (int i = 0; i < temperatures.length; i++) {
while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) {
int top = stack.pop();
res[top] = i - top;
}
stack.push(i);
}
return res;
}
}