美文网首页
2019-08-04

2019-08-04

作者: 张小盒small | 来源:发表于2019-08-04 13:07 被阅读0次
题目描述
image.png

版本一:

"""
# 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
        """
        res = 0
        for item in employees:
            if item.id == id:
                res += item.importance
                if item.subordinates == None:
                    return res
                else:
                    for sub in item.subordinates:
                        res += self.getImportance(employees,sub)
                    
        return res
执行时间

版本二代码优化:

相关文章

网友评论

      本文标题:2019-08-04

      本文链接:https://www.haomeiwen.com/subject/pyaedctx.html