2019-08-04

2019-08-04  本文已影响0人  张小盒small
题目描述
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
执行时间

版本二代码优化:

上一篇下一篇

猜你喜欢

热点阅读