目录
学习目标
学习内容
738.单调递增的数字
968.监控二叉树
总结
学习目标
- 738.单调递增的数字
- 968.监控二叉树
- 总结
学习内容
738.单调递增的数字
738. 单调递增的数字 - 力扣(LeetCode)https://leetcode.cn/problems/monotone-increasing-digits/
class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
s = list(str(n))
for i in range(len(s)-1,0,-1):
if int(s[i])<int(s[i-1]):
s[i-1] = str(int(s[i-1])-1)
s[i:] = ["9"]*(len(s)-i)
return int("".join(s))
968.监控二叉树
968. 监控二叉树 - 力扣(LeetCode)https://leetcode.cn/problems/binary-tree-cameras/
# 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
def dfs(root):
nonlocal res
if not root:return 2
left = dfs(root.left)
right = dfs(root.right)
if left==2 and right==2:
return 0
elif left==0 or right==0:
res+=1
return 1
elif left==1 or right==1:
return 2
if dfs(root)==0:
res+=1
return res
总结
problems/贪心算法总结篇.md · programmercarl/leetcode-master(代码随想录出品) - Gitee.comhttps://gitee.com/programmercarl/leetcode-master/blob/master/problems/%E8%B4%AA%E5%BF%83%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93%E7%AF%87.md#%E8%B4%AA%E5%BF%83%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93%E7%AF%87