mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0496.下一个更大元素I.md
Move solution from 0503.下一个更大元素II
This commit is contained in:
@ -256,6 +256,7 @@ class Solution {
|
|||||||
### Python3
|
### Python3
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
# 版本一
|
||||||
class Solution:
|
class Solution:
|
||||||
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
|
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
|
||||||
result = [-1]*len(nums1)
|
result = [-1]*len(nums1)
|
||||||
@ -273,6 +274,26 @@ class Solution:
|
|||||||
stack.pop()
|
stack.pop()
|
||||||
stack.append(i)
|
stack.append(i)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
# 版本二
|
||||||
|
class Solution:
|
||||||
|
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
|
||||||
|
stack = []
|
||||||
|
# 创建答案数组
|
||||||
|
ans = [-1] * len(nums1)
|
||||||
|
for i in range(len(nums2)):
|
||||||
|
while len(stack) > 0 and nums2[i] > nums2[stack[-1]]:
|
||||||
|
# 判断 num1 是否有 nums2[stack[-1]]。如果没有这个判断会出现指针异常
|
||||||
|
if nums2[stack[-1]] in nums1:
|
||||||
|
# 锁定 num1 检索的 index
|
||||||
|
index = nums1.index(nums2[stack[-1]])
|
||||||
|
# 更新答案数组
|
||||||
|
ans[index] = nums2[i]
|
||||||
|
# 弹出小元素
|
||||||
|
# 这个代码一定要放在 if 外面。否则单调栈的逻辑就不成立了
|
||||||
|
stack.pop()
|
||||||
|
stack.append(i)
|
||||||
|
return ans
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
Reference in New Issue
Block a user