-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_739
31 lines (31 loc) · 972 Bytes
/
leetcode_739
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
//用栈的思想做
//新入的温度放入与栈顶元素比较,如果>栈顶元素
//弹出栈顶元素,并且有一个count计数,记录弹出的元素个数
//否则将新入的温度压栈
stack<pair<int,int>> st;
int len=temperatures.size();
vector<int> ans;
if(len==0)
return ans;
ans.resize(len);
int i=1;
st.push(make_pair(temperatures[0],0));
while(i<len)
{
while(!st.empty()&&temperatures[i]>st.top().first)
{
//cout<<i<<endl;
ans[st.top().second]=i-st.top().second;
st.pop();
//st.push(make_pair(temperatures[i],i));
//i++;
}
st.push(make_pair(temperatures[i],i));
i++;
}
return ans;
}
};