【秋招之战】备战广发银行笔试

在这里插入图片描述

1

import java.util.*;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

public class BinaryTreePaths {
    
    // 条件1:从root到node的路径
    public List<Integer> findPathToNode(TreeNode root, TreeNode node) {
        List<Integer> path = new ArrayList<>();
        if (root == null || node == null) return path;
        
        findPath(root, node, new ArrayList<>(), path);
        return path;
    }
    
    private boolean findPath(TreeNode current, TreeNode target, List<Integer> currentPath, List<Integer> result) {
        if (current == null) return false;
        
        currentPath.add(current.val);
        
        if (current == target) {
            result.addAll(new ArrayList<>(currentPath));
            return true;
        }
        
        if (findPath(current.left, target, currentPath, result) || 
            findPath(current.right, target, currentPath, result)) {
            return true;
        }
        
        currentPath.remove(currentPath.size() - 1);
        return false;
    }
    
    // 条件2:从root到叶子节点且经过node的所有路径
    public List<List<Integer>> findPathsThroughNode(TreeNode root, TreeNode node) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null || node == null) return result;
        
        List<Integer> pathToNode = findPathToNode(root, node);
        if (pathToNode.isEmpty()) return result;
        
        findPathsFromNode(node, new ArrayList<>(), result);
        
        // 将路径拼接:root到node + node到叶子节点(去掉重复的node)
        for (List<Integer> path : result) {
            path.remove(0); // 移除重复的node
            path.addAll(0, pathToNode);
        }
        
        return result;
    }
    
    private void findPathsFromNode(TreeNode node, List<Integer> currentPath, List<List<Integer>> result) {
        if (node == null) return;
        
        currentPath.add(node.val);
        
        if (node.left == null && node.right == null) {
            result.add(new ArrayList<>(currentPath));
        } else {
            findPathsFromNode(node.left, currentPath, result);
            findPathsFromNode(node.right, currentPath, result);
        }
        
        currentPath.remove(currentPath.size() - 1);
    }
    
    public static void main(String[] args) {
        BinaryTreePaths solution = new BinaryTreePaths();
        
        // 构建测试二叉树
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        
        TreeNode target = root.left.right; // 节点5
        
        // 输出条件1
        List<Integer> path1 = solution.findPathToNode(root, target);
        System.out.println("条件1路径: " + path1);
        
        // 输出条件2
        List<List<Integer>> paths2 = solution.findPathsThroughNode(root, target);
        System.out.println("条件2路径:");
        for (List<Integer> path : paths2) {
            System.out.println(path);
        }
    }
}

2

public class ClimbStairs {
    
    public int climbWays(int n) {
        if (n <= 0) return 0;
        if (n == 1) return 1;
        
        // dp[i]表示到达第i格的方法数
        int[] dp = new int[n + 1];
        dp[0] = 1; // 起点
        
        for (int i = 1; i <= n; i++) {
            // 从i-1后退1格到i-2,然后跳2格到i
            if (i - 2 >= 0) {
                dp[i] += dp[i - 2];
            }
            // 从i-1后退1格到i-2,然后跳3格到i+1,但题目要求到第n格
            // 实际上应该是:从i-2后退1格到i-3,然后跳3格到i
            if (i - 3 >= 0) {
                dp[i] += dp[i - 3];
            }
        }
        
        return dp[n];
    }
    
    // 更清晰的解法
    public int climbWays2(int n) {
        if (n < 0) return 0;
        if (n == 0) return 1; // 起点
        
        // 每次操作:后退1格,然后跳2或3格,净前进1或2格
        int[] dp = new int[n + 1];
        dp[0] = 1;
        
        for (int i = 1; i <= n; i++) {
            if (i >= 1) dp[i] += dp[i - 1]; // 净前进1格
            if (i >= 2) dp[i] += dp[i - 2]; // 净前进2格
        }
        
        return dp[n];
    }
    
    public static void main(String[] args) {
        ClimbStairs solution = new ClimbStairs();
        
        for (int i = 1; i <= 10; i++) {
            System.out.println("到达第" + i + "格的方法数: " + solution.climbWays2(i));
        }
    }
}

3

import java.util.*;

public class DirectedGraphPaths {
    
    // 方法1:DFS(适用于无环图或需要所有路径)
    public int countPathsDFS(int[][] edges, int start, int end) {
        // 构建邻接表
        Map<Integer, List<Integer>> graph = new HashMap<>();
        for (int[] edge : edges) {
            graph.computeIfAbsent(edge[0], k -> new ArrayList<>()).add(edge[1]);
        }
        
        return dfs(graph, start, end, new HashSet<>());
    }
    
    private int dfs(Map<Integer, List<Integer>> graph, int current, int end, Set<Integer> visited) {
        if (current == end) {
            return 1;
        }
        
        if (!graph.containsKey(current)) {
            return 0;
        }
        
        int count = 0;
        visited.add(current);
        
        for (int neighbor : graph.get(current)) {
            if (!visited.contains(neighbor)) {
                count += dfs(graph, neighbor, end, visited);
            }
        }
        
        visited.remove(current);
        return count;
    }
    
    // 方法2:动态规划(适用于有向无环图DAG)
    public int countPathsDP(int[][] edges, int n, int start, int end) {
        // 构建邻接表
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            graph.add(new ArrayList<>());
        }
        
        for (int[] edge : edges) {
            graph.get(edge[0]).add(edge[1]);
        }
        
        int[] dp = new int[n + 1];
        dp[end] = 1;
        
        // 拓扑排序(这里简单使用BFS,实际应该用拓扑排序)
        // 对于DAG,我们可以用记忆化搜索
        return memoDFS(graph, start, end, new Integer[n + 1]);
    }
    
    private int memoDFS(List<List<Integer>> graph, int current, int end, Integer[] memo) {
        if (current == end) {
            return 1;
        }
        
        if (memo[current] != null) {
            return memo[current];
        }
        
        int count = 0;
        for (int neighbor : graph.get(current)) {
            count += memoDFS(graph, neighbor, end, memo);
        }
        
        memo[current] = count;
        return count;
    }
    
    public static void main(String[] args) {
        DirectedGraphPaths solution = new DirectedGraphPaths();
        
        // 测试数据
        int[][] edges = {
            {1, 2}, {1, 3}, {2, 3}, {2, 4}, {3, 4}
        };
        int start = 1;
        int end = 4;
        int n = 4; // 节点数量
        
        System.out.println("从" + start + "到" + end + "的路径数量(DFS): " + 
                          solution.countPathsDFS(edges, start, end));
        System.out.println("从" + start + "到" + end + "的路径数量(DP): " + 
                          solution.countPathsDP(edges, n, start, end));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值