738. 单调递增的数字
发现第一位变小了其他的迅速变9
class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
strn=list(str(n))
for i in range(len(strn)-1,0,-1):
if strn[i-1]>strn[i]:
strn[i-1]=str(int(strn[i-1])-1)
for j in range(i,len(strn)):
strn[j]='9'
return int(''.join(strn))
968. 监控二叉树
res是list才能传对象
# 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 = right
class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
res=[0]
if self.tree(root,res)==0:
res[0]+=1
return res[0]
def tree(self,cur,res):
if not cur:
return 2
left=self.tree(cur.left,res)
right=self.tree(cur.right,res)
if left==2 and right==2:
return 0
elif left==0 or right==0:
res[0]+=1
return 1
# if left==1 or right==1:
else:
return 2