未验证 提交 91706f9a 编写于 作者: B BruceCat 提交者: GitHub

Merge pull request #512 from littlecry/master

【739. Daily Temperatures】【Java】
......@@ -181,4 +181,21 @@ vector<int> nextGreaterElements(vector<int>& nums) {
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
\ No newline at end of file
======其他语言代码======
// 739. Daily Temperatures
class Solution {
public int[] dailyTemperatures(int[] T) {
Stack<Integer> stack = new Stack<>();
int[] ans = new int[T.length];
for (int i = 0; i < T.length; i++) {
// 如果压栈之后不满足单调递减,弹出元素,直至保持单调性
while (!stack.isEmpty() && T[i] > T[stack.peek()]) {
int index = stack.pop();
// 被弹出的元素(T[index])都是小于当前的元素(T[i]),由于栈内元素单调递减,大于被弹出元素(index)的最近的就是当前元素(i)
ans[index] = i - index;
}
stack.push(i);
}
return ans;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册