力扣日练-day2
1、盛最多水的容器
关键在于短板往里移可能变为长板容积增大,长板往里移一定变小,双指针,end和start,动态变化指针

class Solution:
def maxArea(self, height: List[int]) -> int:
max_v=0
start=0
end=len(height)-1
while true:
if start<end:
v=min(height[start],height[end])*(end-start)
max_v=max(v,max_v)
if height[start]<height[end]:
start+=1
else:
end-=1
else:
break
return max_v
2、颜色分类
关键在于颜色种类已知,可以利用Counter函数统计每个颜色的数量然后修改原始nums

class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
s=Counter(nums)
for i in range(s[0]):
nums[i]=0
for i in range(s[0],s[0]+s[1]):
nums[i]=1
for i in range(s[0]+s[1],s[0]+s[1]+s[2]):
nums[i]=2
3、最长连续序列

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums=sorted(list(set(nums))
max_len=0
start=0
if len(nums)==0:
return 0
for end in range(len(nums)):
if end-start == nums[end] - nums[start]:
max_len=max(max_len,end-start+1)
else:
start=end
return max_len
4、环形链表
#忽略了题目中的不能修改链表
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
h=head
if h == None:
return head
while h != None:
if h.val != '':
h.val= ''
else:
return h
h = h.next
return None
5、最小栈 列表模拟

class MinStack:
def __init__(self):
self.stack=[]
self.min_stack=[]
def push(self, val: int) -> None:
self.stack.append(val)
if len(self.stack)==1:
self.min_stack.append(val)
else:
self.min_stack.append(min(val,self.min_stack[-1]))
def pop(self) -> None:
self.stack.pop()
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

浙公网安备 33010602011771号