LeetCode-739. Daily Temperatures

网友投稿 287 2022-08-29

LeetCode-739. Daily Temperatures

Given a list of daily temperatures ​​T​​​, return a list such 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 of temperatures ​​T = [73, 74, 75, 71, 69, 72, 76, 73]​​​, your output should be ​​[1, 1, 4, 2, 1, 1, 0, 0]​​.

Note: The length of ​​temperatures​​​ will be in the range ​​[1, 30000]​​​. Each temperature will be an integer in the range ​​[30, 100]​​.

题解:

使用一个递减栈来维护温度高于后者的天数。

class Solution {public: vector dailyTemperatures(vector& T) { if (T.empty() == true) { return {}; } int n = T.size(); vector res(n, 0); stack s; for (int i = 0; i < n; i++) { while (s.empty() == false && T[s.top()] < T[i]) { int t = s.top(); s.pop(); res[t] = i - t; } if (i < n - 1 && T[i] < T[i + 1]) { res[i] = 1; } else { s.push(i); } } return res; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-539. Minimum Time Difference
下一篇:出台互联网营销师职业技能标准的导向意义!(网络营销岗位的技能要求)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~