本文最后更新于589 天前,其中的信息可能已经过时,如有错误请发送邮件到tomding1065@gmail.com
https://programmercarl.com/0739.%E6%AF%8F%E6%97%A5%E6%B8%A9%E5%BA%A6.html
1.本题作为单调栈的第一个题,我还是带着很浓厚的兴趣去看待这个题的,因为本身就是没了解过单调栈的内容所以就直接去看视频了,32分钟的视频直接差点劝退我,我以为多难呢,是卡哥带着我一点一点讲解单调栈里面的关键内容和容易忽视的点,让我在一开始进入到单调栈的世界里有一个好的牢固的基础,思路不难,实现起来也比较简单,很感谢卡哥基本上在这种类型的视频里都会帮我模拟一下这个题的栈的操作过程和另一个存放结果的数组的具体运行的过程,使我受益匪浅。
CPP
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
stack<int> st;
vector<int> answer(temperatures.size(), 0);
st.push(0);
for(int i = 1; i < temperatures.size(); i++){
if(temperatures[i] < temperatures[st.top()]){
st.push(i);
}else if(temperatures[i] == temperatures[st.top()]){
st.push(i);
}else{
while(!st.empty() && temperatures[i] > temperatures[st.top()]){
answer[st.top()] = i - st.top();
st.pop();
}
st.push(i);
}
}
return answer;
}
};