From 480939a1cd59b910071078232bcf4e8ec7c7a97f Mon Sep 17 00:00:00 2001 From: lichun-chen <81766312+lichun-chen@users.noreply.github.com> Date: Thu, 8 Jul 2021 11:00:02 -0500 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B00496.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md=20Python3?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0496.下一个更大元素I.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md index 88e89e52..0404e434 100644 --- a/problems/0496.下一个更大元素I.md +++ b/problems/0496.下一个更大元素I.md @@ -185,3 +185,26 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! +## 其他语言版本 + +Python: +```python3 +class Solution: + def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: + result = [-1]*len(nums1) + stack = [0] + for i in range(1,len(nums2)): + # 情况一情况二 + if nums2[i]<=nums2[stack[-1]]: + stack.append(i) + # 情况三 + else: + while len(stack)!=0 and nums2[i]>nums2[stack[-1]]: + if nums2[stack[-1]] in nums1: + index = nums1.index(nums2[stack[-1]]) + result[index]=nums2[i] + stack.pop() + stack.append(i) + return result +``` +