From 33b488c42fef941083e547d46162d92f6672d714 Mon Sep 17 00:00:00 2001 From: ShusenTang Date: Thu, 7 May 2020 23:14:33 +0800 Subject: [PATCH] add 739. Daily Temperatures :beer: --- README.md | 1 + solutions/739. Daily Temperatures.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 solutions/739. Daily Temperatures.md diff --git a/README.md b/README.md index f6809ee..b18e3a6 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。 | 661 |[Image Smoother](https://leetcode.com/problems/image-smoother)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/661.%20Image%20Smoother.md)|Easy| | | 665 |[Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/665.%20Non-decreasing%20Array.md)|Easy| | | 714 |[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/714.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee.md)|Medium| | +| 739 |[Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)|[C++](solutions/739.%20Daily%20Temperatures.md)|Medium| | | 829 |[Consecutive Numbers Sum](https://leetcode.com/problems/consecutive-numbers-sum/)|[C++](solutions/829.%20Consecutive%20Numbers%20Sum.md)|Hard| | | 846 |[Hand of Straights](https://leetcode.com/problems/hand-of-straights/)|[C++](solutions/846.%20Hand%20of%20Straights.md)|Medium| | | 905 |[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[C++](solutions/905.%20Sort%20Array%20By%20Parity.md)|Easy| | diff --git a/solutions/739. Daily Temperatures.md b/solutions/739. Daily Temperatures.md new file mode 100644 index 0000000..79b085c --- /dev/null +++ b/solutions/739. Daily Temperatures.md @@ -0,0 +1,28 @@ +# [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) + +# 思路 + +数组里每个元素的右边第一个比它大的元素,若找不到用0填充。典型的单调栈,关于单调栈我已经总结过了([戳我](../algorithm/array/monotonic_stack_queue.md)),这里不再赘述,直接给出代码。 + +时空复杂度均为O(N) + +# C++ +``` C++ +class Solution { +public: + vector dailyTemperatures(vector& T) { + int n = T.size(); + vectorres(T.size(), 0); + + stackmono_stk; + for(int i = n - 1; i >= 0; i--){ // 反向遍历 + while(!mono_stk.empty() && T[i] >= T[mono_stk.top()]) + mono_stk.pop(); + if(!mono_stk.empty()) + res[i] = mono_stk.top() - i; + mono_stk.push(i); + } + return res; + } +}; +``` \ No newline at end of file