临桂区住房和城乡建设局门户网站,建设银行信用卡中心网站首页,微信建立免费网站,学做婴儿衣服网站好1. 题目
根据每日 气温 列表#xff0c;请重新生成一个列表#xff0c;对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高#xff0c;请在该位置用 0 来代替。
例如#xff0c;给定一个列表 temperatures [73, 74, 75, 71, 69, 72, 76, …1. 题目
根据每日 气温 列表请重新生成一个列表对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高请在该位置用 0 来代替。
例如给定一个列表 temperatures [73, 74, 75, 71, 69, 72, 76, 73]你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。
提示气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度都是在 [30, 100] 范围内的整数。 来源力扣LeetCode 链接https://leetcode-cn.com/problems/daily-temperatures 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 单调栈解题
class Solution {
public:vectorint dailyTemperatures(vectorint T) {vectorint ans(T.size(),0);stackint stk;for(int i 0; i T.size(); i){while(!stk.empty() T[stk.top()] T[i]){ans[stk.top()] i-stk.top();stk.pop();}stk.push(i);}return ans;}
};class Solution {
public:vectorint dailyTemperatures(vectorint T) {int i, n T.size();vectorint ans(n,0);stackint s;for(i n-1; i 0; --i){while(!s.empty() T[i] T[s.top()])//右边都没有大于我的留着也没用s.pop();//删掉if(!s.empty())ans[i] s.top()-i;s.push(i);}return ans;}
};class Solution:# py3def dailyTemperatures(self, T: List[int]) - List[int]:n len(T)ans [0]*ns []for i in range(n-1,-1,-1):while len(s)0 and T[i] T[s[-1]]:s.pop()if len(s)0:ans[i] s[-1]-is.append(i)return ans572 ms 17.3 MB