• NC_45_THREE_ORDERS NC_46_TARGET_VALUE


    package org.example.interview.practice;
    
    import java.util.ArrayList;
    
    /**
     * @author xianzhe.ma
     * @date 2021/8/30
     */
    
    public class NC_45_THREE_ORDERS {
    
        public static int[][] threeOrders(TreeNode root) {
            // write code here
            ArrayList<TreeNode> list1 = new ArrayList<>();
            ArrayList<TreeNode> list2 = new ArrayList<>();
            ArrayList<TreeNode> list3 = new ArrayList<>();
    
            preOrder(root, list1);
            midOrder(root, list2);
            postOrder(root, list3);
    
            int[][] ret = new int[3][list1.size()];
            int[] pre = new int[list1.size()];
            int[] mid = new int[list1.size()];
            int[] post = new int[list1.size()];
            int index = 0;
            for (TreeNode node : list1) {
                pre[index++] = node.val;
            }
            index = 0;
            for (TreeNode node : list2) {
                mid[index++] = node.val;
            }
            index = 0;
            for (TreeNode node : list3) {
                post[index++] = node.val;
            }
    //        System.arraycopy();
            for (int j = 0; j < list1.size(); j++) {
                ret[0][j] = pre[j];
            }
    
            for (int j = 0; j < list1.size(); j++) {
                ret[1][j] = mid[j];
            }
    
            for (int j = 0; j < list1.size(); j++) {
                ret[2][j] = post[j];
            }
    
            return ret;
        }
    
        private static ArrayList<TreeNode> preOrder(TreeNode root, ArrayList<TreeNode> list) {
            if (root == null) {
                return list;
            }
            list.add(root);
            preOrder(root.left, list);
            preOrder(root.right, list);
            return list;
        }
    
        private static ArrayList midOrder(TreeNode root, ArrayList list) {
            if (root == null) {
                return list;
            }
            midOrder(root.left, list);
            list.add(root);
            midOrder(root.right, list);
            return list;
        }
    
        private static ArrayList postOrder(TreeNode root, ArrayList list) {
            if (root == null) {
                return list;
            }
            postOrder(root.left, list);
            postOrder(root.right, list);
            list.add(root);
            return list;
        }
    
        public static class TreeNode {
            int val = 0;
            TreeNode left = null;
            TreeNode right = null;
    
            public TreeNode(int val) {
                this.val = val;
            }
        }
    
        public static void main(String[] args) {
            TreeNode node1 = new TreeNode(1);
            TreeNode node2 = new TreeNode(2);
            TreeNode node3 = new TreeNode(3);
    
            node1.left = node2;
            node1.right = node3;
    
            threeOrders(node1);
        }
    }
    package org.example.interview.practice;
    
    /**
     * @author xianzhe.ma
     * @date 2021/11/6
     */
    
    import java.util.*;
    
    public class NC_46_TARGET_VALUE {
    
        public static ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
            ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> arr = new ArrayList<Integer>();
            if (num == null || num.length == 0 || target < 0) return res;
            Arrays.sort(num);//对候选数组进行排序 方便后续处理
            dfs(num, target, res, arr, 0);
            return res;
        }
    
        public static void dfs(int[] num, int target, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> arr, int start) {
            if (target == 0) {
                //已找到一组 存储进res
                res.add(new ArrayList<Integer>(arr));
                return;
            }
            if (start >= num.length) return;
            for (int i = start; i < num.length; i++) {
                if (i > start && num[i] == num[i - 1])
                    continue;
                //回溯操作
                if (num[i] <= target) {
                    arr.add(num[i]);
                    dfs(num, target - num[i], res, arr, i + 1);
                    arr.remove(arr.size() - 1);
                }
            }
            return;
        }
    
        public static void main(String[] args) {
            int[] array = {2, 3};
            combinationSum2(array, 5);
        }
    }
  • 相关阅读:
    html,css,javascript 总结
    Java总结2
    Java总结1
    Java学习第5天
    Java学习第4天
    Java学习第3天
    java 学习第二天
    java学习第一天
    markdown 学习
    django——django链接mysql数据库
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15879787.html
Copyright © 2020-2023  润新知