代码随想录二刷day37

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣738. 单调递增的数字
  • 二、力扣968. 监控二叉树


前言


一、力扣738. 单调递增的数字

class Solution {
    public int monotoneIncreasingDigits(int n) {
        String s = String.valueOf(n);
        char[] ch = s.toCharArray();
        int start = s.length();
        for(int i= ch.length-2; i >= 0; i --){
            if(ch[i] > ch[i+1]){
                ch[i] --;
                start = i+1;
            }
        }
        for(int i = start; i < s.length(); i ++){
            ch[i] = '9';
        }
        return Integer.parseInt(String.valueOf(ch));
    }
}

二、力扣968. 监控二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int res = 0;
    public int minCameraCover(TreeNode root) {
        if(fun(root) == 0){
            res ++;
        }
        return res;
    }
    /**
    *0表示当前节点未被覆盖
    *1表示当前节点安装摄像头
    *2表示当前节点已覆盖
    */
    public int fun(TreeNode root){
        if(root == null){
            return 2;
        }
        int left = fun(root.left);
        int right = fun(root.right);
        if(left == 2 &&  right == 2){
            return 0;
        }
        if(left == 0 || right == 0){
            res ++;
            return 1;
            
        }
        if(left == 1 || right == 1){
            return 2;
        }
        
        return 0;
    }
}

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