zoukankan      html  css  js  c++  java
  • 深度优先搜索--计算员工的重要度

    /**
     * 深度遍历的一个简单例子
     *
     */
    public class EmployeeImportance {
        
        private static int res = 0;
        private static Employee employee;
        public static void main(String[] args) {
            Employee e1 = new Employee(1, 5, Arrays.asList(2,3));
            Employee e2 = new Employee(2, 3, null);
            Employee e3 = new Employee(3, 3, null);
            int total = getImportance(Arrays.asList(e1,e2,e3), 1);
            System.out.println(total);
            
        }
        
        
        public static int getImportance(List<Employee> emps, int id) {
            for(Employee emp : emps) {
                if(emp.id == id) {
                    employee = emp;
                    res += emp.value;
                    break;
                }
            }
            List<Integer> list = employee.subordinates;
            if(list != null) {
                for(Integer i : list) {
                    getImportance(emps, i);
                }
            }
            return res;
        }
        
    }
    
    class Employee {
        
        int id;
        int value;
        List<Integer> subordinates;
    
        public Employee(int id, int value, List<Integer> subordinates) {
            super();
            this.id = id;
            this.value = value;
            this.subordinates = subordinates;
        }
    }
  • 相关阅读:
    与您分享
    与您分享
    与您分享
    与您分享
    与您分享
    分享:PythonSIP 4.14.2 发布
    与您分享
    编码
    分享:C++十种方法"Hello World"
    与您分享
  • 原文地址:https://www.cnblogs.com/moris5013/p/11810728.html
Copyright © 2011-2022 走看看