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 = [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
Output: [5,10]
Explanation:
3
/ \
1 5
/
10
Kill 5 will also kill 10.

Note:

  1. The given kill id is guaranteed to be one of the given PIDs.
  2. n >= 1.

这道题让我们结束进程,一直不想翻译程杀死进程,感觉进程很可怜的样子,还要被杀死。题目给了我们两个数组,一个是进程的数组,还有一个是进程数组中的每个进程的父进程组成的数组。题目中说结束了某一个进程,其所有的子进程都需要结束,由于一个进程可能有多个子进程,所以我们首先要理清父子进程的关系。所以我们使用一个哈希表,建立进程和其所有子进程之间的映射,然后我们首先把要结束的进程放入一个队列queue中,然后while循环,每次取出一个进程,将其加入结果res中,然后遍历其所有子进程,将所有子进程都排入队列中,这样我们就能结束所有相关的进程,参见代码如下:

解法一:

class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
vector<int> res;
queue<int> q{{kill}};
unordered_map<int, vector<int>> m;
for (int i = ; i < pid.size(); ++i) {
m[ppid[i]].push_back(pid[i]);
}
while (!q.empty()) {
int t = q.front(); q.pop();
res.push_back(t);
for (int p : m[t]) {
q.push(p);
}
}
return res;
}
};

我们也可以使用递归的写法,思路都一样,只不过用递归函数来代替队列,参见代码如下:

解法二:

class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
vector<int> res;
unordered_map<int, vector<int>> m;
for (int i = ; i < pid.size(); ++i) {
m[ppid[i]].push_back(pid[i]);
}
helper(kill, m, res);
return res;
}
void helper(int kill, unordered_map<int, vector<int>>& m, vector<int>& res) {
res.push_back(kill);
for (int p : m[kill]) {
helper(p, m, res);
}
}
};

参考资料:

https://discuss.leetcode.com/topic/89293/c-clean-code-2-solution-dfs-bfs

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Kill Process 结束进程的更多相关文章

  1. [LeetCode] 582. Kill Process 终止进程

    Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...

  2. LeetCode Kill Process

    原题链接在这里:https://leetcode.com/problems/kill-process/description/ 题目: Given n processes, each process ...

  3. Linux用ps命令查找进程PID再用kill命令终止进程的方法

    使用linux操作系统,难免遇到一些软件"卡壳"的问题,这时就需要使用linux下强大的kill命令来结束相关进程.这在linux系统下是极其容易的事情,你只需要kill xxx即 ...

  4. Linux编程 7 (实时监测进程 top, 结束进程kill,killall)

    一. 实时监测进程 top 在一篇里讲到ps命令在收集进程信息时非常有用,但它只能显示某个特定时间点的信息.想要观察那些频繁换进换出的内存进程趋势,用top命令是合适的.使用top命令如下图所示: 在 ...

  5. ubutu强制结束进程 kill -9 ProcessID

    强制终止进程 kill -9 2128 表示强制结束进程号 2128 对应的进程.

  6. kill 结束进程

    kill 支持的信号 kill -1 重启进程 kill -9 终止进程 pkill 和 killall 的区别在于pkill 可以踢终端用户 pkill  -9  -t tty1

  7. c# 筛选进程命令行,得其ProcessId(唯一标示符,简称pid),再通过pid结束进程

    不说别的,上代码 部分using: using System.Diagnostics; using System.Management; 其中要引用System.Management 1.通过筛选Co ...

  8. Linux常用指令---kill | killall(终止进程)

    kill Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后 ...

  9. Android下结束进程的方法

    转自:http://www.cnblogs.com/crazypebble/archive/2011/04/05/2006213.html 最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这 ...

随机推荐

  1. Restful风格,PUT修改功能请求,表单中存在文件报错-HTTP Status 405 - Request method 'POST' not supported

    解决方案配置如下 <!-- 配置文件上传解析器 --> <bean id="multipartResolver" class="org.springfr ...

  2. Java基础学习笔记九 Java基础语法之this和super

    构造方法 我们对封装已经有了基本的了解,接下来我们来看一个新的问题,依然以Person为例,由于Person中的属性都被private了,外界无法直接访问属性,必须对外提供相应的set和get方法.当 ...

  3. gem devise配置

    Step1: Gemfile中加入gem 'devise' Step3: rails g devise:install 这一步执行完后命令行会提醒要手动进行如下动作: ================ ...

  4. C语言第零次作业总结

    本次作业发现的亮点 没有发现抄袭的现象,大家都是独立且认真地完成这次的作业,希望再接再厉,继续保持 戴洁 陈欢 陈张鑫三位同学的博客写的不错,希望同学们向这三位同学学习,认真对待每次作业 本次作业的问 ...

  5. xcode进行代码覆盖率测试

    去年写的文章,搬到cnblog 本文所述的方法只对xcode5做过测试,xcode6是否可行尚未可知. 配置编译选项 首先请参考苹果官方的文档Configuring Xcode for Code Co ...

  6. django BBS

    https://github.com/triaquae/py_training/tree/master/OldboyBBS2 http://www.cnblogs.com/zhming26/p/592 ...

  7. 小草手把手教你 LabVIEW 串口仪器控制——初识VISA串口

    有些人,学习一样东西时候,喜欢现成的例子.很多人学习一门技术,都喜欢现成的例子开始,比如学单片机的啊,最开始都是修改的例子吧,学语言的也是.最开始都是模仿.这个年头看书上的理论知识太浪费时间了.所以啊 ...

  8. D的下L

    D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2   描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给 ...

  9. 数据结构与算法 —— 链表linked list(01)

    链表(维基百科) 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储, ...

  10. JAVA_SE基础——12.运算符的优先级

    优先级 操作符 含义 关联性 用法 ---------------------------------------------------------------- 1 [ ] 数组下标 左 arra ...