Skip to content

Commit da8f3c9

Browse files
committed
0739 单调栈
1 parent da57f51 commit da8f3c9

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

problems/0739.每日温度.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,36 @@ public:
180180

181181

182182
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+
```
184213
Python:
185214

186215
Go:

0 commit comments

Comments
 (0)