https://leetcode-cn.com/problems/daily-temperatures/
请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。
例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
,你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]
。
提示:气温 列表长度的范围是 [1, 30000]
。每个气温的值的均为华氏度,都是在 [30, 100]
范围内的整数。
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
res = []
n = len(temperatures)
for i in range(n):
for j in range(i+1, n):
if temperatures[j] > temperatures[i]:
res.append(j-i)#
break
else:
res.append(0)
return res
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
res = [0] * n
stack = []
for i in range(n):
while stack and temperatures[i] > temperatures[stack[-1]]:
pre_index = stack.pop()
res[pre_index] = i - pre_index
stack.append(i)
return res
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
n = len(nums2)
stack = []
res_dic = {i:-1 for i in nums2}
res = []
for i in nums2:
while stack and i > stack[-1]:
pre = stack.pop()
res_dic[pre] = i
stack.append(i)
for j in nums1:
res.append(res_dic[j])
return res
[1] 各位兄台,就因为方法一比方法二多了个切片,造成方法一的超时吗
[2] 每日温度