题解:
- 遍历nums1中的每个元素x。
- 找到x在nums2中的下标j。
- 从j+1开始遍历nums2中的元素y,找到第一个大于x的元素y。
- 如果找到了y,将y添加到结果列表ans中;如果没有找到y,将-1添加到结果列表ans中。
- 返回结果列表ans。
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
res = []
for n1 in nums1:
flag = True
index = nums2.index(n1)
for idx in range(index + 1, len(nums2)):
if n1 < nums2[idx]:
flag = False
res.append(nums2[idx])
break
if flag:
res.append(-1)
return res