-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path496_下一个更大元素 I.py
More file actions
35 lines (30 loc) · 1.04 KB
/
496_下一个更大元素 I.py
File metadata and controls
35 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 1 13:37:52 2020
@author: leiya
"""
'''
0710
'''
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
res = [-1 for _ in range(len(nums1))]
for num in nums2:
while stack and num > nums1[stack[-1]]:
res[stack.pop()] = num
if num in nums1:
stack.append(nums1.index(num))
return res
#503变体
#遍历nums2,因为实际上答案的顺序是存在于nums2中的,将nums2中每次遍历到的值在nums1中找到对应的index压入栈中,之后跟739一样
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stack = []
res = [-1 for _ in range(len(nums1))]
for i in range(len(nums2)):
while stack and nums1[stack[-1]] < nums2[i]:
res[stack.pop()] = nums2[i]
if nums2[i] in nums1:
stack.append(nums1.index(nums2[i]))
return res