代码随想录算法训练营Day37

Leetcode738:单调递增的数字

func monotoneIncreasingDigits(n int) int {
    s:=strconv.Itoa(n)
    ss:=[]byte(s)
    l:=len(ss)
    flag:=l
    if l==1{
        return n
    }
    for i:=l-1;i>0;i--{
        if ss[i-1]>ss[i]{
            ss[i-1]--
            flag=i
        }
    }
    for i:=flag;i

Leetcode968:监控二叉树

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
const inf = math.MaxInt64 / 2

func minCameraCover(root *TreeNode) int {
    var dfs func(*TreeNode)(a,b,c int)
    dfs=func(node *TreeNode)(a,b,c int){
        if node==nil{
            return inf,0,0
        }
        lefta,leftb,leftc:=dfs(node.Left)
        righta,rightb,rightc:=dfs(node.Right)
        a=leftc+rightc+1
        b=min(a,min(lefta+rightb,righta+leftb))
        c=min(a,leftb+rightb)
        return
    }
    _,ans,_:=dfs(root)
    return ans
}
func min(a, b int) int {
    if a <= b {
        return a
    }
    return b
}

你可能感兴趣的:(算法,数据结构)