Daily Temperatures问题及解法
来源:互联网 发布:收银台软件 编辑:程序博客网 时间:2024/06/06 07:19
问题描述:
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]
.
Note: The length of temperatures
will be in the range [1, 30000]
. Each temperature will be an integer in the range [30, 100]
.
问题分析:
利用stack存储温度,遇到第一个较大的温度就记录下来。
过程详见代码:
class Solution {public: vector<int> dailyTemperatures(vector<int>& temperatures) { int n = temperatures.size();vector<int> res(n, 0);stack<pair<int, int>> st;for (int i = 0; i < n; i++){while (!st.empty()){if (st.top().first < temperatures[i]){res[st.top().second] = i - st.top().second;st.pop();}else {break;}}st.push(pair<int, int>(temperatures[i], i));}return res; }};
阅读全文
0 0
- Daily Temperatures问题及解法
- 739. Daily Temperatures
- [leetcode] 739. Daily Temperatures
- LeetCode.739 Daily Temperatures
- 739. Daily Temperatures
- 739. Daily Temperatures
- Leetcode 739. Daily Temperatures
- LWC 61:739. Daily Temperatures
- LeetCode刷题 | 739. Daily Temperatures
- leetcode weekly contest 61(739. Daily Temperatures)
- leetcode:739. Daily Temperatures 单调栈
- Heaters问题及解法
- Permutations问题及解法
- Combinations问题及解法
- Subsets问题及解法
- Triangle问题及解法
- Minesweeper问题及解法
- Two Sum问题及解法
- SSL P1194 最优乘车
- 全网最全ASP.NET MVC 教程汇总
- 以产品大白的视角带你体验世界互联网乌镇大会
- Android开发之一键清空内容的输入框
- 我读《启示录.打造用户喜爱的产品》 Marty Cagan著
- Daily Temperatures问题及解法
- cmakelist 草稿一
- JFS简介
- 2141-数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历
- 关于Java 多态的理解及案例
- 大雪来了,怎么样才能暖到心窝里?
- 1060. 爱丁顿数(25) PAT乙级真题
- Linux:进程间通信方式
- Linux网络编程-UDP