Update s0739
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Sainnhe Park 2022-12-16 15:48:54 +08:00
parent 39bb8ffc36
commit 676611e233
1 changed files with 8 additions and 2 deletions

View File

@ -10,10 +10,16 @@ vector<int> S0739::dailyTemperatures(vector<int>& temperatures) {
// 栈中的元素为向量的下标
// 向量下标对应的元素从栈底到栈顶单调递增
stack<int> s;
s.push(0);
// 初始化 ans 中的所有元素为 0
vector<int> ans(len, 0);
for (int i{1}; i < len; ++i) {
for (int i{0}; i < len; ++i) {
// 当栈为空时,直接入栈然后 continue
if (s.empty()) {
s.push(i);
continue;
}
// 当我们发现当前元素比栈顶元素大时,循环弹出
// 注意,是循环弹出,而不是 if 弹出
while (temperatures[i] > temperatures[s.top()]) {
ans[s.top()] = i - s.top();
s.pop();