栈+哈希表
s1是s2的子集
当将要入栈的元素小于栈首元素时入栈,否则先将小于将要入栈元素的所有的在栈内的元素全部出栈,最后再入栈该元素
class Solution(object):
def nextGreaterElement(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
hash_map={}
res=[]
stack=[]
for i in range(0,len(nums2)):
if len(stack)==0:
stack.append(nums2[i])
elif nums2[i]<stack[-1]:
stack.append(nums2[i])
else:
while nums2[i]>stack[-1] and len(stack)>=1:
hash_map[stack[-1]]=nums2[i]
stack.pop()
if len(stack)==0:
break
stack.append(nums2[i])
for i in nums1:
res.append(hash_map.get(i,-1))
return res