代码随想录算法训练营第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
相关推荐
多恩Stone15 分钟前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
xiaopengbc25 分钟前
在 Python 中实现观察者模式的具体步骤是什么?
开发语言·python·观察者模式
Python大数据分析@30 分钟前
python用selenium怎么规避检测?
开发语言·python·selenium·网络爬虫
ThreeAu.34 分钟前
Miniconda3搭建Selenium的python虚拟环境全攻略
开发语言·python·selenium·minicoda·python环境配置
偷心伊普西隆42 分钟前
Python EXCEL 理论探究:格式转换时处理缺失值方法
python·excel
精灵vector2 小时前
LLMCompiler:基于LangGraph的并行化Agent架构高效实现
人工智能·python·langchain
java1234_小锋2 小时前
Scikit-learn Python机器学习 - 特征降维 压缩数据 - 特征选择 - 移除低方差特征(VarianceThreshold)
python·机器学习·scikit-learn
万邦科技Lafite2 小时前
实战演练:通过API获取商品详情并展示
大数据·数据库·python·开放api接口