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. 28-THREE.JS 扇形圆形

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  2. laravel中Blade模板继承

    Blade模板继承 和 区块 <!-- 文件保存于 resources/views/layouts/app.blade.php --> <html> <head> ...

  3. python 在头文件添加 #include \"stdafx.h\"\r\n

    import osimport shutil#-*- coding:cp936 -*-import codecsfrom sys import argv def replace_all_files(p ...

  4. LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. 【git】git知识梳理(二):服务器

    (四) 远程仓库通常只是一个裸仓库(bare repository):.git子目录 四种协议:本地传输, SSH, GIT, HTTP, 除了http,均需要在服务器端安装运行git. SSH:SS ...

  6. java中常用的帮助类。加快开发速度

    数据库帮助类 package com.cwnu.uitl; import java.sql.*; /** * 数据库基础操作实现类 * * @author BlackWinter * * @date ...

  7. UI多线程调用:线程间操作无效: 从不是创建控件"Form1"的线程访问它.

    有两种方式解决 1.在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false;2.使用Invoke等委托函数. 问题原因是.net2.0以后拒绝多 ...

  8. c语言中指针的一个小错误

    在定义指针后需要给指针赋值然后才能使用*p赋值或被赋值,这是个基础问题,没有理解,导致出问题. 空指针 ,也称悬 游指 针 ,是使 用 未初 始化 的指 针 .指针变量未初始化时它的值不是没有 ,而是 ...

  9. 谨慎安装Python3.7.0,SSL低版本导致Pip无法使用

    最新新配置了一台服务器.安装 的时候直接使用了最新的Python 3.7最新版本. 安装成功,编译成功.但是用pip 安装包的时候提示:pip is configured with locations ...

  10. Google搜索被屏蔽,如何使用Google搜索

    我们在国内使用搜索引擎最多的是Google和Baidu啦,在引擎上找一些我们需要的知识,最近好像www.google.cn已经无法访问了,并且香港的链接www.google.com.hk也无法访问了, ...