题目要求

You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id.

For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.

题目分析及思路

定义了一个含有employee信息的数据结构,该结构包括了employee的唯一id、他的重要值以及他的直接下属的id。现给定一个公司的employee的信息,以及一个employee的id,要求返回这个employee以及他所有直接下属的重要值的总和。可以使用递归的方法,跳出递归的条件是当前employee没有直接下属,否则则遍历当前employee的所有下属获得重要值。

python代码

"""

# Employee info

class Employee:

def __init__(self, id, importance, subordinates):

# It's the unique id of each node.

# unique id of this employee

self.id = id

# the importance value of this employee

self.importance = importance

# the id of direct subordinates

self.subordinates = subordinates

"""

class Solution:

def getImportance(self, employees, id):

"""

:type employees: Employee

:type id: int

:rtype: int

"""

ids = [employee.id for employee in employees]

leader = employees[ids.index(id)]

employees.pop(ids.index(id))

if leader.subordinates == []:

return leader.importance

else:

im = 0

for id in leader.subordinates:

im += self.getImportance(employees, id)

return im + leader.importance

LeetCode 690 Employee Importance 解题报告的更多相关文章

  1. 【LeetCode】690. Employee Importance 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...

  2. (BFS) leetcode 690. Employee Importance

    690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...

  3. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

  4. LeetCode 690. Employee Importance (职员的重要值)

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  5. LeetCode - 690. Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  6. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  7. [LeetCode]690. Employee Importance员工重要信息

    哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...

  8. 690. Employee Importance - LeetCode

    Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. Hive数据倾斜解决办法总结

    数据倾斜是进行大数据计算时最经常遇到的问题之一.当我们在执行HiveQL或者运行MapReduce作业时候,如果遇到一直卡在map100%,reduce99%一般就是遇到了数据倾斜的问题.数据倾斜其实 ...

  2. Git 删除操作

    Tom 更新了自己的本地存储库并进入src目录下找到编译后的二进制.查看提交信息后,他意识到,编译后的二进制是由Jerry加入的. . [tom@CentOS src]$ pwd /home/tom/ ...

  3. android中通过intent传递复杂数据

    android中在各个service或者acitivity之间可以通过Intent来传递一些数据,intent原生直接提供了一些简单数据类型的数据的传递,使用起来也很方便,比如int boolean ...

  4. Linux下MySQL5.7.18二进制包安装(无默认配置文件my_default.cnf)

    最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL(MySQL5.7.18)二进制包,CentOS7.2下测试安装,方便以后折腾.大概步骤如下,安装删除反复折腾了几 ...

  5. java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to

    Java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 在使用JSONObject.toBe ...

  6. ActiveMQ JMS 项目 基于 Maven 搭建 部署

    JAVA版本: IntellJ IDEA 版本: IntelliJ IDEA 2017.2Build #IU-172.3317.76, built on July 15, 2017Licensed t ...

  7. Win10系统安装过程小记

    1.网上下载ghost系统http://win10.jysmac.cn/win1064.html 2.使用系统自带的激活工具激活 3.到windows官网下载更新工具更新系统,重新安装https:// ...

  8. C# 解决读取dbf文件,提示Microsoft Jet 数据库引擎找不到对象的问题

    前言 最新项目需要经常和dbf文件打交道,在实际场景中很多软件需要和一些老的系统进行数据交互,而这些系统都在使用foxpro数据库,读取dbf文件一般都是分为两种情况:第一:安装foxpro的驱动进行 ...

  9. 用SpannableString打造绚丽多彩的文本显示效果

    extends:http://www.jianshu.com/p/84067ad289d2 引语 TeXtView大家应该都不陌生,文本展示控件嘛! 就用TextView显示普普通通的文本,OK,很简 ...

  10. css---计算页面的的宽度和长度

    我们在写前端页面的时候,会遇到这样的情况,就是一个div设置宽度100%,设置左右边距10像素,这样的布局,在里面嵌套的div的宽度设置100%,这样写的话,里面的宽度是和外面的宽度一致的,同样是10 ...