0
点赞
收藏
分享

微信扫一扫

leetcode第292场周赛,题目2264&2265

无愠色 2022-05-10 阅读 53
java

2264

class Solution {
    public String largestGoodInteger(String num) {
        int n=num.length();
        char max=' ';
        for(int i=0;i<n-2;i++){
            if(num.charAt(i)==num.charAt(i+1)&&num.charAt(i+1)==num.charAt(i+2)){
                if(max!=' '){
                    if(num.charAt(i)>max){
                        max=num.charAt(i);
                    }
                }
                else{
                    max=num.charAt(i);
                }
            }
        }
        String res="";
        if(max!=' '){
            res+=max;
            res+=max;
            res+=max;
        }
        return res;
    }
}

2265

class Solution {
    int res=0;
    public int averageOfSubtree(TreeNode root) {
        dfs(root);
        return res;
    }
    //返回和、节点数
    public int[] dfs(TreeNode root){
        if(root==null)  return new int[]{0,0};
        int[] left=dfs(root.left);
        int[] right=dfs(root.right);
        int sum=left[0]+right[0]+root.val;
        int count=left[1]+right[1]+1;
        int avg=Math.round(sum/count);
        if(avg==root.val){
            this.res++;
        }
        return new int[]{sum,count};
    }
}
举报

相关推荐

0 条评论