739. Daily Temperatures
Given a list of daily temperatures
, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0
instead.
For example, given the list temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
, your output should be [1, 1, 4, 2, 1, 1, 0, 0]
.
给一组数,对每个数寻找之后出现的较大值,并返回坐标差。
跟之前有一题比较像,用一个vector存一下还没找到较大值的数就行了。
class Solution { public: vector<int> dailyTemperatures(vector<int>& temperatures) { vector<int> res(temperatures.size(), 0); vector<pair<int, int>> rec; for (int i=0; i<temperatures.size(); ++i) { while (!rec.empty() && (rec.back().first < temperatures[i])) { res[rec.back().second] = i - rec.back().second; rec.pop_back(); } rec.push_back(make_pair(temperatures[i], i)); } return res; } };