代码随想录算法训练营第36天|● 738.单调递增的数字 ● 968.监控二叉树

738. 单调递增的数字

发现第一位变小了其他的迅速变9

python 复制代码
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才能传对象

python 复制代码
# 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
相关推荐
Hylan_J2 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
莫忘初心丶2 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
失败尽常态5235 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447745 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农5 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿5 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing6 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
xinxiyinhe7 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语7 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3
橘子师兄8 小时前
分页功能组件开发
数据库·python·django