690. Employee Importance

  一开始写的很麻烦,是因为忘记了auto的使用,auto适合做这种数据结构比较复杂的题。使用auto一般要结合for迭代循环,即for(:)可以参考一篇博文  https://blog.youkuaiyun.com/u010141928/article/details/78671452

该题的题意多少有些模糊,其实是求不仅是直接下属的所有员工的weight的和,还有直接下属的员工的直接下属的员工。

class Solution {
public:
    int sum=0;
    void dfs(vector<Employee*> employees, int id)
    {
        for(auto temp : employees) //employees 是个保存Employee* 值的vector,所以auto的temp是Employee*类型
        {
            if(temp->id==id) 
            {
                sum +=temp->importance;
                if(!temp-> subordinates.empty())//加一个非空判断是很有必要的
                {
                    for(auto sub_id : temp-> subordinates)//subordinates是个保存int类型(id)的vector,所以auto的sub_id是int类型
                    {
                        dfs(employees, sub_id);
                    }
                }


            }
        }
    }
    
    int getImportance(vector<Employee*> employees, int id) {
        dfs(employees, id);
        return sum;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值