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 ...
随机推荐
- 24-THREE.JS 镜面高光材质
<!DOCTYPE html> <html> <head> <title>Example 04.07 - Mesh Phong material< ...
- 【nyoj-1274】信道安全(SPFA)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1274 题目描述 Alpha 机构有自己的一套网络系统进行信息传送.情报员 A 位于 ...
- mybatis分页插件使用注意
之前的项目用的数据库是mysql,在pom.xml引入这一个就能分页了. 后来又开了一个项目,使用的是oracle数据库,我写分页的时候发现不能实现,在网上找到资料说是必须要5.0以上的.我就导了这依 ...
- LeetCode OJ:Remove Duplicates from Sorted List (排好序的链表去重)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 软工作业-Wc
Wc.exe wc.exe是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数.单词数和行 ...
- int 21h 汇编
INT 21H 指令说明及使用方法 转自http://www.cnblogs.com/ynwlgh/archive/2011/12/12/2285017.html 很多初学汇编语言的同学可能会对INT ...
- CBP是什么?
coded_block_pattern 简称CBP,用来反映该宏块编码中残差情况的语法元素.CBP共有6位,其中前面2位代表UV分量,描述如下表所示:后面4位是Y分量,分别代表宏块内的4个8x8子宏 ...
- java调用cmd执行maven命令
一.原理介绍 Java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后封闭命令窗口. cmd /k di ...
- => 应用在js回调函数中
=> 可以简化以前的回调函数的调用,具体来说: 今后,几乎所有的回调函数都可用箭头函数简化 比如: 1. 所有回调函数都可: 去function改=> 2. 如果函数体只有一句话: 可省略 ...
- vc++6 Platform SDK February 2003
vc++6.0 sp6 ftp://ejiasoft:softejia@ejia2.tust.edu.cn/else/VC++.6.0.with.SP6.ISO MSDN http://ftp.sds ...