public class Solution {
public IList<int> KillProcess(IList<int> pid, IList<int> ppid, int kill)
{
if (kill == )
{
return pid;
} int n = pid.Count;
Dictionary<int, List<int>> tree = new Dictionary<int, List<int>>();
for (int i = ; i < n; i++)
{
tree.Add(pid[i], new List<int>());
}
for (int i = ; i < n; i++)
{
if (tree.ContainsKey(ppid[i]))
{
var children = tree[ppid[i]];
children.Add(pid[i]);
if (!tree.ContainsKey(ppid[i]))
{
tree.Add(ppid[i], children);
}
}
} List<int> result = new List<int>();
traverse(tree, result, kill); return result;
} private void traverse(Dictionary<int, List<int>> tree, List<int> result, int pid)
{
result.Add(pid); var children = tree[pid];
foreach (var child in children)
{
traverse(tree, result, child);
}
}
}

https://leetcode.com/problems/kill-process/#/solutions

tree中记录的是每个进程及其直接的子进程。然后在调用traverse方法,这个方法是递归的进行寻找,每个级别的进程及其子进程,将其全部加入到要删除的进程列表result中。

leetcode582的更多相关文章

  1. 杀死进程-LeetCode-582

    英文版 582. Kill ProcessGiven n processes, each process has a unique PID (process id) and its PPID (par ...

  2. 奇安信集团笔试题:二叉树的最近公共祖先(leetcode236),杀死进程(leetcode582)

    1. 二叉树最近公共祖先     奇安信集团 2020校招 服务端开发-应用开发方向在线考试 编程题|20分2/2 寻祖问宗 时间限制:C/C++语言 1000MS:其他语言 3000MS 内存限制: ...

  3. LeetCode-Queue

    简单题 1. 数据流中的移动平均值 $(leetcode-346) 暂无 2. 最近的请求次数(leetcode-933) 写一个 RecentCounter 类来计算最近的请求. 它只有一个方法:p ...

随机推荐

  1. 全局ajax事件

    必须当页面上存在任何ajax请求的时候都将触发这些特定的全局ajax处理函数. 如果在jQuery.ajaxSetup()中的global属性设置成true,那么这些全局函数将会在每一个ajax上面都 ...

  2. Node多进程相关

    现状: 目前使用child_process.fork实现多进程,一个center.js负责任务分配及相关状态管理,一个worker.js负责任务执行. center.js结构: 1) 一个task_q ...

  3. Django框架(三)

    0627内容: 上节回顾: 1. FBV.CBV 2. 数据库操作 class UserGroup(models.Model): """ 部门 3 "" ...

  4. scrapy 碎片

    1.启动命令 2.目录结构 3.文件说明 4.架构图示 5.代码流程 参考资料: http://www.cnblogs.com/yangxt90/articles/9021530.html http: ...

  5. 打印机无法使用且无法重新安装,提示spooler service is not running

    使用场景:之前安装好的打印服务今天突然无法使用,列表里面找不到打印机,于是重新安装,得到以下错误: The local print spooler service is not running. Pl ...

  6. windows 下多线程

    unsigned uiThread2ID; HANDLE handle = (HANDLE)_beginthreadex(NULL, , ThreadUploadFun, NULL, CREATE_S ...

  7. 旧书重温:0day2【11】第6章 狙击windows的异常处理

    昨晚经过一番努力,又把第六章的内容温习了一遍! 随手,做了一个实验!狙击windows的异常处理, 顺便也把过程记录了下来!省事!(有图) 今早,论坛一直无法打开! 就推迟到了现在! 哈哈 正题: 第 ...

  8. 【操作系统】总结五(I/O管理)

    输入输出管理本章主要内容: I/O管理概述(I/O控制方式.I/O软件层次结构)和I/O核心子系统(I/O调度概念.局速缓存与缓冲区.设备分配与回收.假脱机技术(SPOOLing)). 5.1 I/O ...

  9. hexo搭建个人主页托管于github

    之前学习了 如何利用Github免费搭建个人主页,今天利用hexo来快速生成个人网页托管于github上. hexo系列教程:(一)hexo介绍 什么是hexo hexo是一个基于Node.js的静态 ...

  10. week01—绪论

    一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...