classSolution:defsummaryRanges(self, nums: List[int])-> List[str]:
res =[]ifnot nums:return res
low =0
high =0
n =len(nums)while high < n :while high < n and nums[high]- nums[low]== high - low:
high +=1if high == low +1:
res.append(str(nums[low]))else:
res.append(str(nums[low])+"->"+str(nums[high-1]))
low = high
return res
283.移动零
classSolution:defmoveZeroes(self, nums: List[int])->None:"""
Do not return anything, modify nums in-place instead.
"""
n =len(nums)
left, right =0,0for right inrange(n):print(left,right)if nums[right]:
nums[left], nums[right]= nums[right], nums[left]# 将0交换到right, 0一直在left和right之间
left +=1# 每交换一次,就让left进1,保证left之前的都是非0,return
543.二叉树的直径
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassSolution:defdiameterOfBinaryTree(self, root: TreeNode)->int:
self.depth =1deftree_depth(node):ifnot node:return0# 到底了,返回深度为0
L = tree_depth(node.left)# 查找左儿子的深度
R = tree_depth(node.right)# 查找右儿子的深度
self.depth =max(self.depth, L+R+1)# 知道左右儿子深度之后,将他们加起来再加1(自己)作为本node以下的深度,与查询到的其他node下面的深度做对比,留最大的returnmax(L, R)+1# 返回自己左或者右儿子的最大深度,+1是自己
tree_depth(root)# 调用函数求root最大深度return self.depth-1#深度-1是直径
228.汇总区间 283.移动零 543.二叉树的直径228.汇总区间class Solution: def summaryRanges(self, nums: List[int]) -> List[str]: res = [] if not nums: return res low = 0 high = 0 n = len(nums) whil..