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 ...
随机推荐
- LeetCode OJ:4Sum(4数字之和)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- android实现log日志输出
1.下载android的log4j的库(的封装) 去: http://code.google.com/p/android-logging-log4j/ 下载对应的 android-logging-lo ...
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- Arcgis for Javascript实现两个地图的联动
今天在看天地图的时候,有一个多时相的地图显示功能,感觉很好玩,作为技术控的我晚上十点下班到家便是快十一点了,本来应该是睡觉了,但是,激动地心情不能平静,哎,算了,本着不熬夜的程序员不是好程序员的原则, ...
- C/C++ 安全编码 —— 指针与内存
1. 仿踩内存 if (buf[len - 1] != 0x5A) { return; }
- 前端之jQuery02
文档操作 重点:创建标签,jQuery里面没有这个方法 内部(子标签) 添加到指定元素内部后面 $(A).append(B): // B作为A的最后一个儿子元素:(把B追加到A) $(A).appen ...
- 基于UDP协议编程
基于udp套接字 udp是无链接的,先启动哪一端都不会报错. UDP(user datagram protocol,用户数据报协议)是无连接的,面向消息的,提供高效率服务.不会使用块的合并优化算法,, ...
- ACM学习历程—计蒜客15 单独的数字(位运算)
http://nanti.jisuanke.com/t/15 题目要求是求出只出现一次的数字,其余数字均出现三次. 之前有过一个题是其余数字出现两次,那么就是全部亦或起来就得到答案. 这题有些不太一样 ...
- devops 几个方便的工具
1. fake API [canned](https://github.com/sideshowcoder/canned ) fake API. [wiremock](ht ...
- vuecli3修改项目启动端口
工作中可能存在启动多个项目的时候,默认端口号会被占,导致启动错误,这种情况下只要把要启动的项目的端口号换掉启动未用的端口就可以了,具体实现如下: vuecli3中的端口文件存放目录为:node_mod ...