Skip to content

Commit

Permalink
Update 0503.下一个更大元素II.md
Browse files Browse the repository at this point in the history
move second solution to 0496.下一个更大元素I
  • Loading branch information
0zz10 authored Jan 21, 2024
1 parent 9928779 commit 68cdbdb
Showing 1 changed file with 0 additions and 21 deletions.
21 changes: 0 additions & 21 deletions problems/0503.下一个更大元素II.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ class Solution {
### Python:

```python
# 方法 1:
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
dp = [-1] * len(nums)
Expand All @@ -181,26 +180,6 @@ class Solution:
stack.pop()
stack.append(i%len(nums))
return dp

# 方法 2:
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:

Expand Down

0 comments on commit 68cdbdb

Please sign in to comment.