739. 每日温度
题目链接:739. 每日温度
代码如下:
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures)
{
vector<int> res(temperatures.size(),0);
stack<int> sta;//递增栈
sta.push(0);
for(int i=1;i<temperatures.size();i++)
{
if(temperatures[i]<temperatures[sta.top()])//当前遍历的元素T[i]小于栈顶元素T[st.top()]的情况
sta.push(i);
else if(temperatures[i]==temperatures[sta.top()])//当前遍历的元素T[i]等于栈顶元素T[st.top()]的情况
sta.push(i);
else//当前遍历的元素T[i]大于栈顶元素T[st.top()]的情况
{
while(!sta.empty()&&temperatures[i]>temperatures[sta.top()])
{
res[sta.top()]=i-sta.top();
sta.pop();
}
sta.push(i);
}
}
return res;
}
};