力扣爆刷第160天之TOP100五连刷66-70(回溯、旋转图像、技巧题)

力扣爆刷第160天之TOP100五连刷66-70(回溯、旋转图像、技巧题)

一、110. 平衡二叉树

题目链接:https://leetcode.cn/problems/balanced-binary-tree/description/
思路:判断二叉树是否是平衡二叉树,只需要判断每一个节点的左右子树高度差是否大于1,既然需要每个节点左右子树的信息,自然得是后序遍历才能拿到,所以采用后序遍历,以返回左右子树的最大高度进行判断即可。

class Solution {
    boolean flag = true;
    public boolean isBalanced(TreeNode root) {
        isTrue(root);
        return flag;
    }

    int isTrue(TreeNode root) {
        if(root == null || !flag) return 0;
        int left = isTrue(root.left);
        int right = isTrue(root.right);
        if(Math.abs(left - right) > 1) flag = false;
        return Math.max(left, right) + 1;
    }
}

二、39. 组合总和

题目链接:https://leetcode.cn/problems/combination-sum/description/
思路:对于一个无重复元素的数组,但是每个元素可以使用的次数是不限制的,求组合成一个目标数的,所有组合方式。组合和排序是典型的回溯题目,组合因为不讲究前后顺序,所以需要指定起始位置,才不会出现重复的组合,又因为单个元素可以重复使用,所以起始位置是当前元素即可,如果不可复用,则是下一个位置。其他的都一样,注意排列早停。

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    int sum = 0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        backTracking(candidates, target, 0);
        return result;
    }
    
    void backTracking(int[] nums, int target, int index) {
        if(sum == target) {
            result.add(new ArrayList(list));
            return;
        }
        for(int i = index; i < nums.length && nums[i] + sum <= target; i++) {
            sum += nums[i];
            list.add(nums[i]);
            backTracking(nums, target, i);
            sum -= nums[i];
            list.remove(list.size()-1);
        }
    }
    
}

三、543. 二叉树的直径

题目链接:https://leetcode.cn/problems/diameter-of-binary-tree/description/
思路:求二叉树的直径,说的是可能过根节点,也可能不过,其实最大直径求的就是每一个节点的左右子树高度和,那么就简单了,就是求树的高度,然后加和左右子树高度,然后记录即可。

class Solution {
    int max = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        traverse(root);
        return max;
    }
    int traverse(TreeNode root) {
        if(root == null) return 0;
        int left = traverse(root.left);
        int right = traverse(root.right);
        max = Math.max(max, left + right);
        return Math.max(left, right) + 1;
    }
}

四、470. 用 Rand7() 实现 Rand10()

题目链接:https://leetcode.cn/problems/implement-rand10-using-rand7/description/
思路:本题让用产生1-7的随机数函数,构造产生1-10随机数的函数,其实很构造,只需要1/2 * 1/5即可。
构造1/2:要最求尽可能的是1/2,一般来说,范围够大就行,用while循环产生数,只要大于6就一直循环,那么退出while循环时,出来的数只能是1-6,3个奇数,3个偶数,那么判断是奇数还是偶数就构造了1/2.
构造1/5:只需要while生成数,大于5的不要,产生的就是1-5,那么就可以根据奇数还是偶数来决定是否给+5。
由此即可构造Rand10()。

/**
 * The rand7() API is already defined in the parent class SolBase.
 * public int rand7();
 * @return a random integer in the range 1 to 7
 */
class Solution extends SolBase {
    public int rand10() {
        int first, second;
        while((first = rand7()) > 6) ;
        while((second = rand7()) > 5) ;
        return (first & 1) == 1 ? second : 5 + second;
    }
}

五、48. 旋转图像

题目链接:https://leetcode.cn/problems/rotate-image/description/
思路:90度旋转矩阵,也是很经典的题目,其实只需要沿着左上角与右下角的独角线先对称交换一下,在沿着竖直中线左右交换一下即可完成向右90度旋转。
在这里插入图片描述

class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        
        for(int i = 0; i < n; i++) {
            for(int j = i; j < n; j++) {
                int t = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = t;
            }
        }
        
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n/2; j++) {
                int t = matrix[i][j];
                matrix[i][j] = matrix[i][n-j-1];
                matrix[i][n-j-1] = t;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当年拼却醉颜红

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值