1.LeetCode 739 每日温度
题目链接:739. 每日温度
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
answer = [0] * len(temperatures)
stack = [0]
for i in range(1, len(temperatures)):
while len(stack) != 0 and temperatures[i] > temperatures[stack[-1]]:
answer[stack[-1]] = i - stack[-1]
stack.pop()
stack.append(i)
return answer
第一题结束
2.LeetCode 496 下一个更大元素 I
题目链接:496. 下一个更大元素 I
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]]:
if nums2[stack[-1]] in nums1:
index = nums1.index(nums2[stack[-1]])
ans[index] = nums2[i]
stack.pop()
stack.append(i)
return ans
第二题结束
3.LeetCode 503 下一个更大元素 II
题目链接:503. 下一个更大元素 II
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stack = [0]
a = len(nums)
nums2 = nums * 2
ans = [-1] * len(nums2)
for i in range(1, len(nums2)):
while len(stack) != 0 and nums2[i] > nums2[stack[-1]]:
ans[stack[-1]] = nums2[i]
stack.pop()
stack.append(i)
return ans[:a]
第三题结束
今天用时1.5h

1万+

被折叠的 条评论
为什么被折叠?



