应用题-单调栈专题-下一个更大元素I

管理员
```python class Solution: # 对nums1中的每个元素x,在nums2中找到x的位置, # 然后找出该位置右侧第一个比 x 大的数;如果不存在,返回 -1。最终返回与 nums1 等长的答案数组。 def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: # nums1和nums2中所有整数 互不相同 standard = dict() for v in nums2: standard[ v ] = -1 # 构造nums2的下一个更大元素 # 求下一个更大元素 保证栈顶比栈中所有元素都大 st = [] for v in nums2: while st and v > st[ -1 ]: val = st.pop() standard[ val ] = v st.append( v ) # 构造nums1的下一个更大元素 ans = [] for v in nums1: ans.append( standard[ v ] ) return ans ```
评论 0

发表评论 取消回复

Shift+Enter 换行  ·  Enter 发送
还没有评论,来发表第一条吧