马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
今日专题:单调栈
739. 每日温度
- class Solution:
- def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
- #单调栈
- size=len(temperatures)
- st=[]
- res=[0]*size
- for i in range(len(temperatures)-1,-1,-1):
- while len(st)>0 and temperatures[i]>=temperatures[st[-1]]:
- st.pop()
- if len(st)>0:
- res[i]=st[-1]-i
- st.append(i)
- return res
-
复制代码 496. 下一个更大元素 I
- class Solution:
- def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
- # 1.在nums2中找到nums1[i]的位置j
- # 2. 找到j后面的第一个更大元素
- #直接先维护出nums2中每个元素的后一个元素
- st=[]
- dict={}
- size=len(nums2)
- for i,n in enumerate(reversed(nums2)):
- while len(st)>0 and n>=st[-1]:
- st.pop()
- if len(st)>0:
- dict[n]=st[-1]
- else:
- dict[n]=-1
- st.append(n)
- res=[]
- for n in nums1:
- res.append(dict[n])
- return res
复制代码 503. 下一个更大元素 II
- class Solution:
- def nextGreaterElements(self, nums: List[int]) -> List[int]:
- #拷贝一个数组拼到原数组后面
- size=len(nums)
- nums=nums+nums[::]
- st=[]
- res=[-1]*size
- for i in range(len(nums)-1,-1,-1):
- while len(st)>0 and nums[i]>=st[-1]:
- st.pop()
- if len(st)>0 and i<size:
- res[i]=st[-1]
- st.append(nums[i])
- return res
-
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |