代码随想录算法训练营第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
相关推荐
山烛6 分钟前
KNN 算法中的各种距离:从原理到应用
人工智能·python·算法·机器学习·knn·k近邻算法·距离公式
guozhetao19 分钟前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
墨染点香25 分钟前
第七章 Pytorch构建模型详解【构建CIFAR10模型结构】
人工智能·pytorch·python
阿什么名字不会重复呢1 小时前
在线工具+网页平台来学习和操作Python与Excel相关技能
python·数据分析
Vertira1 小时前
python 阿里云 安装 dashscope的简介、安装
开发语言·python
gc_22992 小时前
学习Python中Selenium模块的基本用法(1:简介)
python·selenium
先做个垃圾出来………3 小时前
2116. 判断一个括号字符串是否有效
python
兮℡檬,3 小时前
房价预测|Pytorch
人工智能·pytorch·python
im_AMBER7 小时前
学习日志19 python
python·学习
mortimer10 小时前
安装NVIDIA Parakeet时,我遇到的两个Pip“小插曲”
python·github