好几种写法,这里贴几个出来

第一种:暴力解法,除去递归栈,空间复杂度O(1)。时间复杂度略高

 /*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int getImportance(vector<Employee*> employees, int id)
{
return getsum(employees,id);
} int getsum(vector<Employee*>employees,int id)
{
Employee * cur;
int cursum=;
for(auto p:employees)
{
if(p->id==id)
{
cur=p;
cursum+=p->importance;
break;
}
}
if(!(cur->subordinates.empty()))
{
for(auto subid:cur->subordinates)
cursum+=getsum(employees,subid);
}
return cursum;
}
};

第二种:用关联容器,速度快一丢丢,但是空间复杂度高一丢丢

 /*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int getImportance(vector<Employee*> employees, int id)
{
unordered_map<int,Employee*> iemap;
for(auto i:employees)
iemap[i->id]=i;
return getsum(iemap,id);
} int getsum(unordered_map<int,Employee*> & emap,int id)
{
int cursum=emap[id]->importance;
for(auto e:emap[id]->subordinates)
cursum+=getsum(emap,e);
return cursum;
}
};

第三种,用数组,空间复杂度比map高些,但是速度更快

 /*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int getImportance(vector<Employee*> employees, int id)
{
Employee* ptr[]={nullptr};
for(auto p:employees)
ptr[p->id]=p;
return getsum(ptr,id);
} int getsum(Employee* (&ptr)[],int id)
{
int cursum=ptr[id]->importance;
for(auto e:ptr[id]->subordinates)
cursum+=getsum(ptr,e);
return cursum;
}
};

三种方法都可以,都用了递归

690. Employee Importance的更多相关文章

  1. (BFS) leetcode 690. Employee Importance

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

  2. LN : leetcode 690 Employee Importance

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

  3. 【Leetcode_easy】690. Employee Importance

    problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...

  4. 690. Employee Importance - LeetCode

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

  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 解题报告

    题目要求 You are given a data structure of employee information, which includes the employee's unique id ...

  7. [LeetCode&Python] Problem 690. Employee Importance

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

  8. 690. Employee Importance员工权限重要性

    [抄题]: You are given a data structure of employee information, which includes the employee's unique i ...

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

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

随机推荐

  1. CUDA error 100 & Decoder not initialized

    项目中用cuda解码时候遇到该错误,这是调用cuda相关库中一些so库版本错误造成的.

  2. poj 2553 缩点+染色+出度

    题目链接:https://vjudge.net/problem/POJ-2553 如果不会tarjan算法,推荐博客:https://blog.csdn.net/mengxiang000000/art ...

  3. POJ-3078.Shuffle'm Up(简单模拟题)

    这道题做了有四个小时吧,今天一整天都处于边玩边学的状态,我很是不喜欢...一开始用了20分钟模拟,过了样例后TLE了,就在考虑是不是判断是否重复判定的数组开大了,结果一直蛙,后面想到了map判重,结果 ...

  4. checkbox/radio 样式修改

    只改颜色 input[type=radio],input[type=checkbox] { display: inline-block; vertical-align: middle; width: ...

  5. 【转】收集 jetty、tomcat、jboss、weblogic 的比较

    jetty Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开 ...

  6. ES3之cookie

    一 cookie的作用域由文档源(domain)和文档路径(path)决定. 当前页面的脚本只能操作:当前源的当前路径的cookie.当前源的父路径的cookie. 向服务器发送请求时,请求头(Req ...

  7. Mac电脑Dock栏开启放大特效

    1 右击Dock栏空白处,选择启用放大 2 在Dock偏好设置中调整图标放大的倍数 3 滑动鼠标,查看放大效果

  8. swift - scrollview 判断左右移动, 以及上下两个view联动

    核心代码 1. 2. 3. 界面代码VFL /* 浏览作品view*/ import UIKit /** * 图片浏览器(大图和缩略图) */ class JYBrowseWorksView: UIV ...

  9. MD5加密和sha加密

    sha加密原理Algorithm)又叫安全哈希加密技术,是当今世界最先近的加密算法.主要用于文件身份识别.数字签名和口令加密等. 对于明文信息A,通过SHA1算法,生成一条160位长的识别码B.且明文 ...

  10. 【linux】下Apache无法启动(8080端口被占用)

    Linux下8080端口被占用,apache无法启动. 打开终端输入netstat -lnp|grep 8080 发现竟然是tcp6 占用里,因此ipv6启用占用了端口. 1.打开/etc/sysct ...