编程题(二叉树的遍历和深度、最大子数组、最大子矩阵)----2018.08.01

package m08d01;

/**
 * @author JackarooZhang
 * @date 2018/8/1 16:58
 */
class TreeNode<T> {
    TreeNode left;
    TreeNode right;
    T data;
    public TreeNode(T data) {
        this.data = data;
    }
}

public class Item01 {

    public static void main(String[] args) {
        TreeNode<Integer> root = new TreeNode<>(5);
        root.left = new TreeNode(4);
        root.right = new TreeNode(3);
        root.right.left = new TreeNode(0);
        root.left.left = new TreeNode(2);
        root.left.left.left = new TreeNode(1);

        int md = minDepth(root);
        System.out.println(md);
    }

    /**
     * 求二叉树的最小深度
     * @param root
     * @return
     */
    public static int minDepth(TreeNode<?> root) {
        if (root == null) return 0;
        int leftMH = minDepth(root.left);
        int rightMH = minDepth(root.right);
        return (leftMH == 0 || rightMH == 0) ? (leftMH + rightMH + 1) : (Math.min(leftMH, rightMH) + 1);
    }

    /**
     * 先序遍历
     * @param root
     */
    public static void firstErgodic(TreeNode<?> root) {
        if (root == null) return;
        System.out.println(root.data.toString());
        firstErgodic(root.left);
        firstErgodic(root.right);
    }


}
package m08d01;

/**
 * @author JackarooZhang
 * @date 2018/8/1 17:23
 */
public class Item02 {

    public static void main(String[] args) {
        int[] a = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        System.out.println(maxSubArray2(a));
    }

    /**
     * 求数组中最大子数组
     * @param nums
     * @return
     */
    public static int maxSubArray2(int[] nums) {
        int n = nums.length;
        int current = nums[0];
        int max = nums[0];
        //我们考虑如果全是负数,那么返回最大的负数,如果最后的和为正,那么就使用扫描法
        for (int i = 1; i < nums.length; i++) {
            if (current < 0) current = nums[i];//当前数小于0 肯定会舍去(否则将会影响接下来的和),换为下一个数
            else current += nums[i];//如果当前数不小于0,那么他会对接下来的和有积极影响
            if (current > max) max = current;//这里既实现了负数返回最大也实现了扫描法
            //这里其实已经隐式的列举了所有可能,保留了所有可能的最大值
        }
        return max;
    }

    public static int maxSubArray(int[] nums) {
        int n = nums.length;
        int max = 0;
        for (int i = 0; i < n; i++) { // i 为左端点
            int sum = 0;
            for (int j = i; j < n; j++) { // j 为右端点
                sum += nums[j];
                max = max < sum ? sum : max;
            }
        }
        return max;
    }

    public static int maxSumInArray(int[] nums) {
        int n = nums.length;
        int max = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                int sum = sum(nums, i, j);
                max = sum > max ? sum : max;
            }
        }

        return max;
    }

    /**
     * 求数组nums中[start, end)中元素的和
     *
     * @param nums
     * @param start
     * @param end
     * @return
     */
    public static int sum(int[] nums, int start, int end) {
        int sum = 0;
        for (int i = start; i < end; i++) {
            sum += nums[i];
        }
        return sum;
    }

}
package m08d01;

/**
 * @author JackarooZhang
 * @date 2018/8/1 22:19
 */
public class Item03 {

    public static void main(String[] args) {
        int matrix[][] = {
                {0, -2, -7, 0},
                {9, 2, -6, 2},
                {-4, 1, -4, 1},
                {-1, 8, 0, -2},
        };
        int max = maxMatrix(matrix);
        System.out.println(max);
    }

    /**
     * 最大子矩阵
     * @param matrix
     * @return
     */
    public static int maxMatrix(int matrix[][]) {
        if (matrix == null || matrix.length == 0)
            return 0;
        int max = 0;
        int col = matrix[0].length, row = matrix.length;
        for (int i = 0; i < row; i++) {
            int arr[] = new int[col];
            for (int j = i; j < row; j++) {
                //遍历所有的子行
                for (int k = 0; k < col; k++) {
                    arr[k] += matrix[j][k];
                    //将每子行的值进行相加然后利用子数组的最大和就可以求出子矩阵的最大和
                }
                max = Math.max(maxSubArray(arr), max);
                //求出数组的子数组和最大值
            }
        }
        return max;
    }

    public static int maxSubArray(int[] nums) {
        int n = nums.length;
        int current = nums[0];
        int max = nums[0];
        //我们考虑如果全是负数,那么返回最大的负数,如果最后的和为正,那么就使用扫描法
        for (int i = 1; i < nums.length; i++) {
            if (current < 0) current = nums[i];//当前数小于0 肯定会舍去(否则将会影响接下来的和),换为下一个数
            else current += nums[i];//如果当前数不小于0,那么他会对接下来的和有积极影响
            if (current > max) max = current;//这里既实现了负数返回最大也实现了扫描法
            //这里其实已经隐式的列举了所有可能,保留了所有可能的最大值
        }
        return max;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值