Study Plan For Algorithms - Part42
1. 删除排序链表中的重复元素
给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
curr = head
while curr.next:
if curr.val == curr.next.val:
curr.next = curr.next.next
else:
curr = curr.next
return head
2. 柱状图中最大的矩形
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
stack = []
heights = [0] + heights + [0]
max_area = 0
for i, height in enumerate(heights):
while stack and height < heights[stack[-1]]:
mid = stack.pop()
left = stack[-1]
right = i
width = right - left - 1
max_area = max(max_area, width * heights[mid])
stack.append(i)
return max_area
本文来自博客园,作者:WindMay,转载请注明原文链接:https://www.cnblogs.com/stephenxiong001/p/18431065