杀死进程-LeetCode-582
英文版
582. Kill Process
Given n processes, each process has a unique PID (process id) and its PPID (parent process id).
Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.
We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.
Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for
the final answer.
Example 1:
Input:
pid = [, , , ]
ppid = [, , , ]
kill =
Output: [,]
Explanation: / \ / Kill will also kill .
Note:
- - The given kill id is guaranteed to be one of the given PIDs.
- - n >= 1.
中文版
给你n个进程,每个进程都有一个唯一的PID(process id)和PPID(parent process id)。每个进程最多只有一个父进程,但可能有多个子进程。如果一个进程的PPID为0,表示它没有父进程。如果一个进程被杀死,那么它的子进程也会被杀死。输入两个相同长度的整数链表,第一个链表是每个进程的PID,第二个链表是对应位置进程的PPID,再输入一个将被杀死的进程的PID,请输出所有将被杀死的进程的PID。
例如,输入的PID链表为[1, 3, 10, 5],PPID链表为[3, 0, 5, 3],将被杀死的进程的PID为5,那么最终被杀死的进程有两个,PID分别为5和10。这是因为PID为10的进程是PID为5的进程的子进程。
分析
由于每个进程最多只有一个父进程,但可能有多个子进程,因此进程之间的关系可以用一棵树来表示。父进程对应树中的父节点,而子进程对应树中的子节点。如果进程[1, 3, 10, 5]的父进程为[3, 0, 5, 3],那么这些进程可以构成如下的一棵树:如样例中的树结构
因此,解决这个问题的第一步是根据输入的PID和PPID两个链表构建出一个树。树是一种特殊的图(Graph)。图有两种常用的表示方法,一是基于邻接表,二是基于邻接矩阵。由于树是一类比较稀疏的图,一般用邻接表更为高效。
用邻接表来表示一棵树,我们可以把整棵树存到一个HashMap中。这个HashMap的Key为进程的PID,Value为对应进程的所有子进程的PID,也就是树中对应节点的所有子节点。下面是根据PID和PPID链表构建树的参考代码:
private Map<Integer, List<Integer>> buildTree(List<Integer> pid, List<Integer> ppid) {
Map<Integer, List<Integer>> processTree = new HashMap<>();
Iterator<Integer> iterator1 = pid.iterator();
Iterator<Integer> iterator2 = ppid.iterator();
while (iterator1.hasNext() && iterator2.hasNext()) {
int p = iterator1.next();
int pp = iterator2.next();
if (!processTree.containsKey(pp)) {
processTree.put(pp, new LinkedList<>());
}
processTree.get(pp).add(p);
}
return processTree;
}
由于杀死一个进程会杀死它的所有子进程,那么对应到树中,杀死某个节点对应的进程,也就会杀死以该节点为根节点的子树中所有节点对应的所有进程。因此,我们可以把问题转化为:如何遍历以某节点为根节点的子树。
虽然这棵由进程构成的树不一定是二叉树(因为节点的子节点数目可能大于2),但遍历算法大同小异,仍然可以按照广度优先或者深度优先的顺序遍历。下面分别介绍两种方法的代码。
深度优先解
树的深度优先搜索的代码有递归和非递归两种写法。通常基于递归的代码更为简洁,深度优先算法需要定义一个递归函数用来杀死某个进程,如下面的辅助函数kill。在函数kill中,每当杀死进程target,如果它有子进程,则递归调用函数kill去杀死它的每一个子进程child。
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int target){
Map<Integer, List<Integer>> processTree = buildTree(pid, ppid);
List<Integer> result = new LinkedList<>();
kill(processTree, target, result);
return result;
}
private void kill(Map<Integer, List<Integer>> processTree, int target, List<Integer> result) {
result.add(target);
if (!processTree.containsKey(target)) {
return ;
}
for (int child : processTree.get(target)) {
kill(processTree, child, result);
}
}
也可以不构建树结构直接,利用这两个数组进行递归杀死进程。
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
ArrayList<Integer> ans = new ArrayList<Integer>();
ans.add(kill);
for (int i = 0; i < ppid.size(); i++) {
if (ppid.get(i) == kill) {
ans.addAll(killProcess(pid, ppid, pid.get(i)));
}
}
return ans;
}
广度优先解
基于广度优先的代码通常会用到队列(queue)。我们首先把第一个待杀死的进程的PID放入队列中,接下来只要队列中还有待杀死的进程,就重复执行如下步骤:首先从队列中去除一个进程,杀死它(即添加到输出的链表中);接着,如果该进程有子进程则把子进程添加到队列中。
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int target) {
Map<Integer, List<Integer>> processTree = buildTree(pid, ppid));
List<Integer> result = new LinkedList<>();
Queue<Integer> queue = new LinkedList<>();
queue.add(target);
while (!queue.isEmpty()) {
int process = queue.remove();
result.add(process);
if (processTree.containsKey(process)) {
for (int child : processTree.get(process)) {
queue.add(child);
}
}
}
return result;
}
杀死进程-LeetCode-582的更多相关文章
- 奇安信集团笔试题:二叉树的最近公共祖先(leetcode236),杀死进程(leetcode582)
1. 二叉树最近公共祖先 奇安信集团 2020校招 服务端开发-应用开发方向在线考试 编程题|20分2/2 寻祖问宗 时间限制:C/C++语言 1000MS:其他语言 3000MS 内存限制: ...
- sql杀死进程
查询SQL所有的链接 并可以查看连接当前正在做什么操作..使用的什么语句.. SELECT spid, blocked, DB_NAME(sp.dbid) AS DBName, program_na ...
- Window通过cmd查看端口占用、相应进程、杀死进程
在windows下启动程序时候经常出现端口占用, 修改本程序端口是一种解决思路,但是更多时候希望直接杀掉占用端口的程序: 一. 查看所有进程占用的端口 在开始-运行-cmd,输入: netsta ...
- Linux Shell脚本实现根据进程名杀死进程
Shell脚本源码如下: #!/bin/sh #根据进程名杀死进程 if [ $# -lt 1 ] then echo "缺少参数:procedure_name" exit 1 f ...
- Linux如何查看进程、杀死进程、启动进程等常用命令
Linux如何查看进程.杀死进程.启动进程等常用命令 关键字: linux 查进程.杀进程.起进程1.查进程 ps命令查找与进程相关的PID号: ps a 显示现行终端机下的所有程序,包括 ...
- Window 通过cmd查看端口占用、相应进程、杀死进程等的命令【转】
一. 查看所有进程占用的端口 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 二.查看占用指定端口的程序 当你在用tomcat发布程序时,经常会遇到端口被占用的情况,我们想知 ...
- windows下根据端口号杀死进程
Windows不像Linux,Unix那样,ps -ef 查出端口和进程号,然后根据进程号直接kill进程. Windows根据端口号杀死进程要分三步: 第一步 根据端口号寻找进程号 C:\>n ...
- linux 杀死进程的方法
# kill -pid 注释:标准的kill命令通常都能达到目的.终止有问题的进程,并把进程的资源释放给系统.然而,如果进程启动了子进程,只杀死父进程,子进程仍在运行,因此仍消耗资源.为了防止这些所谓 ...
- centos(linux) 下如何查看端口占用情况及杀死进程
使用这个命令:netstat -nap [root@Jaosn sphinx]# netstat -nap Active Internet connections (servers and estab ...
随机推荐
- 解决git commit 遇到datached HEAD问题
git detached HEAD 你可以认为 HEAD(大写)是”current branch”(当下的分支).当你用git checkout切换分支的时候,HEAD 修订版本重新指向新的分支. 有 ...
- 【hdu4609】 3-idiots FFT
题外话:好久没写blog了啊-- 题目传送门 题目大意:给你m条长度为ai的线段,求在其中任选三条出来,能构成三角形的概率.即求在这n条线段中找出三条线段所能拼出的三角形数量除以$\binom{m}{ ...
- h5仿微信、支付宝数字键盘|微信支付键盘|支付宝付款键盘
html5仿微信支付数字键盘|仿支付宝键盘|h5仿微信密码输入键盘|自定义数字键盘 很早之前由于项目需求,就有开发过一个h5仿微信支付键盘,这几天就把之前的数字键盘模块独立出来,重新整理开发成demo ...
- Machine learning 第8周编程作业 K-means and PCA
1.findClosestCentroids function idx = findClosestCentroids(X, centroids) %FINDCLOSESTCENTROIDS compu ...
- spring自定义注解拦截器的配置
1.创建注解文件 (文件格式为注解) 这里面什么都不需要写 文件名就是注解名称 如下 是@anno package com.ABC123.anno; import java.lang.annotati ...
- CentOS7 yum方式安装 MongoDB 3.4 复制集
CentOS7 yum方式安装 MongoDB 3.4 环境.准备 Centos7 系统 配置MongoDB的yum源,添加文件/etc/yum.repos.d/mongodb-org-3.4.rep ...
- ASP.NET:EntityFramework实现Session
ASP.NET默认的InProc模式的Session既浪费内存又在网站重启时存在数据丢失问题,SQLServer模式的Session只支持SQL Server又需要命令行配置.使用EntityFram ...
- Android4.0 Launcher拖拽原理分析
在Android4.0源码自带的Launcher中,拖拽是由DragController进行控制的. 1) 先来看看类之间的继承关系 2)再来看看Launcher拖拽流程的时序图 1.基本流程: ...
- 关于Spring的一点东西
Spring IoC 容器 容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁.Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件.这些对象被称为 Spr ...
- gitHub-高仿58同城加载动画
导入方式: /build.gradle repositories { maven { url "https://jitpack.io" } } /app/build.gradle ...