Skip to content

Commit

Permalink
Merge pull request youngyangyang04#381 from minghaha/master
Browse files Browse the repository at this point in the history
0739 单调栈
  • Loading branch information
youngyangyang04 authored Jun 11, 2021
2 parents ca2e8aa + da8f3c9 commit 2c24d81
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion problems/0739.每日温度.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,36 @@ public:


Java:

```java
/**
* 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到笑
* <p>
* 入站元素要和当前栈内栈首元素进行比较
* 若大于栈首则 则与元素下标做差
* 若大于等于则放入
*
* @param temperatures
* @return
*/
public static int[] dailyTemperatures(int[] temperatures) {
Stack<Integer> stack = new Stack<>();
int[] res = new int[temperatures.length];
for (int i = 0; i < temperatures.length; i++) {
/**
* 取出下标进行元素值的比较
*/
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
int preIndex = stack.pop();
res[preIndex] = i - preIndex;
}
/**
* 注意 放入的是元素位置
*/
stack.push(i);
}
return res;
}
```
Python:

Go:
Expand Down

0 comments on commit 2c24d81

Please sign in to comment.