标签:str 题解 ade public amp problems tac contain lock
(LeetCode 739. 每日温度)[https://leetcode-cn.com/problems/daily-temperatures/]
Reference: https://leetcode-cn.com/problems/daily-temperatures/solution/mei-ri-wen-du-by-leetcode/
T[0]=10; T[10]=20; T[20]=20;则T[20]不会是T[0]答案T[0]=10; T[10]=10; T[20]=20;则T[20]可能是T[0]答案!st.empty()&&T[i]>=T[stack.top()],则一直执行pop(),此处必须是>=,因为温度相等不能算提升,因此需要pop()0,否则为stack.top()-it=[73,74,75,71,69,72,76,73]
i = 7,ans[i] = 0,stack = [7 (73)]。i = 6,pop(), ans[i] = 0, stack = [6 (76)]。i = 5,ans[i] = 1, stack = [5 (72), 6 (76)]。i = 4,ans[i] = 1, stack = [4 (69), 5 (72), 6 (76)]。i = 3,pop(),ans[i] = 2, stack = [3 (71), 5 (72), 6 (76)]。class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
if(T.empty()) return {};
stack<int> st;
vector<int> res(T.size(),0);
for(int i=T.size()-1;i>=0;i--)
{
while(!st.empty()&&T[st.top()]<=T[i]) st.pop();
res[i]=st.empty()?0:st.top()-i;
st.push(i);
}
return res;
}
};
标签:str 题解 ade public amp problems tac contain lock
原文地址:https://www.cnblogs.com/HurryXin/p/12701612.html