This commit is contained in:
27
src/s0739_daily_temperatures.cpp
Normal file
27
src/s0739_daily_temperatures.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "s0739_daily_temperatures.hpp"
|
||||
|
||||
vector<int> S0739::dailyTemperatures(vector<int>& temperatures) {
|
||||
int len = temperatures.size();
|
||||
if (len == 0) {
|
||||
return vector<int> {};
|
||||
} else if (len == 1) {
|
||||
return vector<int> {0};
|
||||
}
|
||||
// 栈中的元素为向量的下标
|
||||
// 向量下标对应的元素从栈底到栈顶单调递增
|
||||
stack<int> s;
|
||||
s.push(0);
|
||||
// 初始化 ans 中的所有元素为 0
|
||||
vector<int> ans(len, 0);
|
||||
for (int i{1}; i < len; ++i) {
|
||||
while (temperatures[i] > temperatures[s.top()]) {
|
||||
ans[s.top()] = i - s.top();
|
||||
s.pop();
|
||||
if (s.empty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
s.push(i);
|
||||
}
|
||||
return ans;
|
||||
}
|
Reference in New Issue
Block a user