leetcode582
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的更多相关文章
- 杀死进程-LeetCode-582
英文版 582. Kill ProcessGiven n processes, each process has a unique PID (process id) and its PPID (par ...
- 奇安信集团笔试题:二叉树的最近公共祖先(leetcode236),杀死进程(leetcode582)
1. 二叉树最近公共祖先 奇安信集团 2020校招 服务端开发-应用开发方向在线考试 编程题|20分2/2 寻祖问宗 时间限制:C/C++语言 1000MS:其他语言 3000MS 内存限制: ...
- LeetCode-Queue
简单题 1. 数据流中的移动平均值 $(leetcode-346) 暂无 2. 最近的请求次数(leetcode-933) 写一个 RecentCounter 类来计算最近的请求. 它只有一个方法:p ...
随机推荐
- asp.net获取URL和IP地址
(转自:http://www.cnblogs.com/JuneZhang/archive/2010/11/26/1888863.html) HttpContext.Current.Request.Ur ...
- C#—序列化(Serialize)和反序列化(NonSerialize)
(转自:http://www.cnblogs.com/Abel-Zhang/p/Serialize.html) 一.概述 序列化是把对象转变成流.相反的过程就是反序列化. 哪些场合用到这项技术呢? 1 ...
- 【2018年全国多校算法寒假训练营练习比赛(第四场)-D】小明的挖矿之旅
题目链接:https://www.nowcoder.com/acm/contest/76/D 做题时没注意到“无论出现在哪个格子”..题中也没说明一个格子只能经过一次,其实没有想象的复杂. 判断如果点 ...
- log4j文件的配置
public class TestLog4j { /** * 级别从大到小 * fatal * error * warn * info * debug * trace * * off:不打印任何信息! ...
- H5 项目问题总结
//一.HTML页面结构 <meta name="viewport" content="width=device-width,initial-scale=1.0,m ...
- Java一般要学多久?
其实学java一般要多久?因人而异,有些人资质好,头脑聪明几个月就能学会,有些人天生愚钝,理解能力差,不过勤能补拙,只是时间相对长点 要坚持住.不过java相对于C,C++java而言,java无疑简 ...
- 人生苦短之我用Python篇(paramiko模块)
该模块机遇SSH用于连接远程服务器并执行相关操作 基于用户名密码连接: import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在kno ...
- 使用jenkins并发布应用到tomcat
jenkins的介绍及安装请自行百度,本文重点介绍如何使用jenkins,并自动发布web应用到tomcat中. 1 . 创建项目 打开jenkins --> 新建 --> 填写item名 ...
- SSH项目配置数据源的方法(jndi)
1.在tomcat6.0/conf/context.xml加入以下代码 [xhtml] view plain copy <Resource name="jdbc/oracleD ...
- wifi文件传输
步骤: 1.下载CocoaHTTPServer 2.解压后,将CocoaHTTPServer-master目录下的Core导入工程. 3.打开Samples/SimpleFileUploadServe ...