|
|
@@ -0,0 +1,32 @@
|
|
|
+package leetcode.p739;
|
|
|
+
|
|
|
+import java.util.Stack;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ProjectName: LeetCode
|
|
|
+ * @FileName: Solution
|
|
|
+ * @Author: 杨逸
|
|
|
+ * @Data:2024/10/15 9:51
|
|
|
+ * @Description: https://leetcode.cn/problems/daily-temperatures/description/
|
|
|
+ * 739. 每日温度
|
|
|
+ */
|
|
|
+public class Solution {
|
|
|
+ public int[] dailyTemperatures(int[] temperatures) {
|
|
|
+ //单调栈
|
|
|
+ int[] ans = new int[temperatures.length];
|
|
|
+ //存索引
|
|
|
+ Stack<Integer> stack = new Stack();
|
|
|
+ for (int i = 0; i < temperatures.length; i++) {
|
|
|
+ if (stack.isEmpty()){
|
|
|
+ stack.push(i);
|
|
|
+ }else {
|
|
|
+ while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
|
|
|
+ int index = stack.pop();
|
|
|
+ ans[index] = i - index;
|
|
|
+ }
|
|
|
+ stack.push(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ans;
|
|
|
+ }
|
|
|
+}
|