题81(中等):
python代码:
class Solution:
def search(self, nums: List[int], target: int) -> bool:
# 显然与折半查找类似嘛
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return True
# 判断左侧是否有序
if nums[mid] > nums[left]:
# target在左侧范围内
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
elif nums[mid] == nums[left]:
left += 1
else:
# target在右侧范围内
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return False
题82(中等):
python代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
#做一个伪表头
new_head=ListNode(None,head)
q,p=new_head,head
if p==None:
return head
flag=0
while p.next!=None:
if p.val==p.next.val:
print(p.val)
p.next=p.next.next
flag=1
else:
if flag==1:
q.next=p.next
p=p.next
else:
q=p
p=p.next
flag=0
if flag==1:
q.next=p.next
p=p.next
return new_head.next
题83(简单):
python代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
p=head
if p==None:
return p
while p.next!=None:
if p.val==p.next.val:
p.next=p.next.next
else:
p=p.next
return head
题84(困难)
分析
这题我开始思路就是遍历强算,确定左右计算面积。但是时间复杂度肯定是n^2,所以没过,难度其实挺高,然后我看评论,单调栈?啥玩意,不懂,不过现在学了一下。我们知道如果算面积肯定用最小的那个作为高,所以我们就找比它矮的,一旦遇到就进行面积计算,因为这个时候带着这个新来的高度肯定要变,所以我们依次和以径进栈的高度比高,找到不用变高度的程度。还有一点,这道题应该是要记录下标(来计算宽度)
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
h_stack=[]
max_res=0
#使用单调栈来求解
for i in range(len(heights)):
#如果这个元素大于栈顶
if h_stack==[] or heights[i]>=heights[h_stack[-1]]:
h_stack.append(i)
#如果小于栈顶
else:
#将元素依次出栈直到遇到比它大的
while 1:
#如果出完了
if h_stack==[]:
break
if heights[i]>heights[h_stack[-1]]:
break
else:
element=h_stack.pop()
if h_stack==[]:
width=i
else:
width=i-h_stack[-1]-1
max_res=max(max_res,heights[element]*width)
h_stack.append(i)
while 1:
# 如果出完了
if h_stack == []:
break
else:
element = h_stack.pop()
if h_stack==[]:
width=len(heights)
else:
width=len(heights) - h_stack[-1]-1
max_res = max(max_res, heights[element] * width)
return max_res
题85(困难):
分析:
好家伙,又来一道难度高的题目,要不是我想到上一题就搞不出来了
python代码
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
h_stack = []
max_res = 0
# 使用单调栈来求解
for i in range(len(heights)):
# 如果这个元素大于栈顶
if h_stack == [] or heights[i] >= heights[h_stack[-1]]:
h_stack.append(i)
# 如果小于栈顶
else:
# 将元素依次出栈直到遇到比它大的
while 1:
# 如果出完了
if h_stack == []:
break
if heights[i] > heights[h_stack[-1]]:
break
else:
element = h_stack.pop()
if h_stack == []:
width = i
else:
width = i - h_stack[-1] - 1
max_res = max(max_res, heights[element] * width)
h_stack.append(i)
while 1:
# 如果出完了
if h_stack == []:
break
else:
element = h_stack.pop()
if h_stack == []:
width = len(heights)
else:
width = len(heights) - h_stack[-1] - 1
max_res = max(max_res, heights[element] * width)
return max_res
def maximalRectangle(self, matrix: List[List[str]]) -> int:
row = len(matrix)
col = len(matrix[0])
res=0
height=[0 for i in range(col)]
for i in range(row):
for j in range(col):
if matrix[i][j]=="1":
height[j]+=1
else:
height[j]=0
res=max(res,self.largestRectangleArea(height))
return res