Pārlūkot izejas kodu

#feat:leetcode p739题题解

yangyi 2 nedēļas atpakaļ
vecāks
revīzija
348d8f8f00
1 mainītis faili ar 32 papildinājumiem un 0 dzēšanām
  1. 32 0
      src/leetcode/p739/Solution.java

+ 32 - 0
src/leetcode/p739/Solution.java

@@ -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;
+    }
+}