[leetcode-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 = [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:
The given kill id is guaranteed to be one of the given PIDs.
n >= 1.
思路:
用一个map去对应父节点与它的所有子节点,map<int,set<int>>。子节点放到set里面。
然后用深度优先遍历的方法去遍历。DFS。
void killCore(map<int, set<int>>& nodes, vector<int>& ret, int kill)
{
ret.push_back(kill);
//set<int>::iterator it;
//for (it = nodes[kill].begin(); it != nodes[kill].end();it++)
//{
// killCore(nodes, ret, *it);
//}
for (int child : nodes[kill])
{
killCore(nodes, ret, child);
}
}
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill)
{
vector<int> ret;
map<int,set<int>> nodes;//每一个父结点对应的子结点
for (int i = ; i < pid.size();i++)
{
nodes[ppid[i]].insert(pid[i]);
}
killCore(nodes, ret, kill); return ret;
}
参考:
https://leetcode.com/problems/kill-process/#/solutions
[leetcode-582-Kill Process]的更多相关文章
- [LeetCode] 582. Kill Process 终止进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- 582. Kill Process杀死所有子代
[抄题]: Given n processes, each process has a unique PID (process id) and its PPID (parent process id) ...
- 582. Kill Process
Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...
- linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析
从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...
- Kill Process by Name
Kill Process by Name(works in: Microsoft Windows 95/98/ME/NT/2000/XP)It is sometimes necessary to te ...
- Mongodb副本集--Out of memory: Kill process 37325 (mongod)
1.Mongodb副本集--Out of memory: Kill process 37325 (mongod) 场景描述: 恢复一个22TB数据的mongodb实例的时候. 将备用结点加入mongo ...
- 理解和配置Out of memory: Kill process
转自:爱开源 理解 OOM killer 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通 ...
- Out of memory: Kill process 内存不足
服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Failed to add the deployment content to the ...
- Android Kill Process
/********************************************************************** * Android Kill Process * 说明: ...
- Out of memory: Kill process 6033 (mysqld) score 85 or sacrifice child
进入正题前先说明:OOM killer什么时候出现? linux下允许程序申请比系统可用内存更多的内存,这个特性叫Overcommit.这样做是出于优化系统考虑,因为不是所有的程序申请了内存就立刻使用 ...
随机推荐
- jdbc3
- Java过滤敏感词语/词汇---DFA算法
最近网站需要在评论.投稿等地方过滤敏感词汇,于是在网上查找了相关教程,特此整理分享. 关于DFA算法,详细的可以去http://blog.csdn.net/u013378306/article/det ...
- PHP平台CMS系统Drupal小试身手----安装教程
最近一直在研究基于Asp.Net MVC的CMS---Orchard,忽然新血来潮,看看多年不看的PHP平台的CMS,那好,就拿Drupal试试身手吧. 第一大招: 环境配置 + 安装. 1.环境配置 ...
- 弹性盒布局display:flex详解
一:弹性盒子 随着响应式设计的流行,网站开发者在设计网页布局时往往要考虑到页面在适配不同分辨率的浏览器时其内部组件位置大小都会产生变化,因此需要设计者根据窗口尺寸来调整布局,从而改变组件的尺寸和位置, ...
- Nginx教程(四) Location配置与ReWrite语法
Nginx教程(四) Location配置与ReWrite语法 1 Location语法规则 1.1 Location规则 语法规则: location [=|~|~*|^~] /uri/ {- } ...
- cuda学习1-初始庐山真面目
cuda作为gpu计算中的代表,拥有着超级高的计算效率,其原因是gpu实际相当与一台超级并行机组,使用过MPI做并行计算的人们可能知道,所谓的并行计算,简单讲就是用多个U(计算单元)来完成一个U的计算 ...
- R – GPU Programming for All with ‘gpuR’
INTRODUCTION GPUs (Graphic Processing Units) have become much more popular in recent years for compu ...
- 【Netty】ChannelHandler和codec
一.前言 前面学习了Netty的codec框架,下面接着学习ChannelHandler与codec之间的关联. 二.ChannelHandler和codec Netty为不同的协议提供了处理器和编解 ...
- react 的五脏六腑ing~
用react一年多了.一直是在别人的影子下写的代码,他们也确实都是大神级的人物,不过,小菜鸟也有小菜鸟的思想~这不,今天就在重温一遍react!记一些零碎的知识点~不知道对你们有没有用,不过,对于我, ...
- AngularJS入门第一课
1.ng-app=" " 定义angularJS的使用范围: 2.ng-init="变量=值;变量='值'" 初始化变量的值,有多个变量时,中间用分号隔 ...