File tree Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -180,7 +180,36 @@ public:
180
180
181
181
182
182
Java:
183
-
183
+ ``` java
184
+ /**
185
+ * 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到笑
186
+ * <p >
187
+ * 入站元素要和当前栈内栈首元素进行比较
188
+ * 若大于栈首则 则与元素下标做差
189
+ * 若大于等于则放入
190
+ *
191
+ * @param temperatures
192
+ * @return
193
+ */
194
+ public static int [] dailyTemperatures(int [] temperatures) {
195
+ Stack<Integer > stack = new Stack<> ();
196
+ int [] res = new int [temperatures. length];
197
+ for (int i = 0 ; i < temperatures. length; i++ ) {
198
+ /**
199
+ * 取出下标进行元素值的比较
200
+ */
201
+ while (! stack. isEmpty() && temperatures[i] > temperatures[stack. peek()]) {
202
+ int preIndex = stack. pop();
203
+ res[preIndex] = i - preIndex;
204
+ }
205
+ /**
206
+ * 注意 放入的是元素位置
207
+ */
208
+ stack. push(i);
209
+ }
210
+ return res;
211
+ }
212
+ ```
184
213
Python:
185
214
186
215
Go:
You can’t perform that action at this time.
0 commit comments