class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
# 主要思路当前数字比前面数字小时。前面数字 -1,当前数字变2为 9
str_n = str(n)
for i in range(len(str_n)-1, 0, -1):
if str_n[i] < str_n[i-1]:
str_n = str_n[:i-1] + str(int(str_n[i-1]) - 1) + len(str_n[i:]) * "9"
return int(str_n)
class Solution:
def minCameraCover(self, root: Optional[TreeNode]) -> int:
# 0 代表无覆盖
# 1 代表有摄像头
# 2 代表有覆盖
self.res = 0
if self.travesal(root) == 0:
self.res += 1
return self.res
def travesal(self, cur):
if not cur:
return 2
# 左
left = self.travesal(cur.left)
# 右
right = self.travesal(cur.right)
# 分析当前节点的状态
# 中
# 左右都被覆盖
if left == 2 and right == 2:
return 0
# 左或者右有一个没有被覆盖
if left == 0 or right == 0:
self.res += 1
return 1
# 左或者右右摄像头
if left == 1 or right == 1:
return 2
标签:right,return,Python,self,int,第三十七,str,left,二叉树
From: https://www.cnblogs.com/yixff/p/17834226.html