[LeetCode] 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.
这道题让我们结束进程,一直不想翻译程杀死进程,感觉进程很可怜的样子,还要被杀死。题目给了我们两个数组,一个是进程的数组,还有一个是进程数组中的每个进程的父进程组成的数组。题目中说结束了某一个进程,其所有的子进程都需要结束,由于一个进程可能有多个子进程,所以我们首先要理清父子进程的关系。所以我们使用一个哈希表,建立进程和其所有子进程之间的映射,然后我们首先把要结束的进程放入一个队列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 结束进程的更多相关文章
- [LeetCode] 582. Kill Process 终止进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- LeetCode Kill Process
原题链接在这里:https://leetcode.com/problems/kill-process/description/ 题目: Given n processes, each process ...
- Linux用ps命令查找进程PID再用kill命令终止进程的方法
使用linux操作系统,难免遇到一些软件"卡壳"的问题,这时就需要使用linux下强大的kill命令来结束相关进程.这在linux系统下是极其容易的事情,你只需要kill xxx即 ...
- Linux编程 7 (实时监测进程 top, 结束进程kill,killall)
一. 实时监测进程 top 在一篇里讲到ps命令在收集进程信息时非常有用,但它只能显示某个特定时间点的信息.想要观察那些频繁换进换出的内存进程趋势,用top命令是合适的.使用top命令如下图所示: 在 ...
- ubutu强制结束进程 kill -9 ProcessID
强制终止进程 kill -9 2128 表示强制结束进程号 2128 对应的进程.
- kill 结束进程
kill 支持的信号 kill -1 重启进程 kill -9 终止进程 pkill 和 killall 的区别在于pkill 可以踢终端用户 pkill -9 -t tty1
- c# 筛选进程命令行,得其ProcessId(唯一标示符,简称pid),再通过pid结束进程
不说别的,上代码 部分using: using System.Diagnostics; using System.Management; 其中要引用System.Management 1.通过筛选Co ...
- Linux常用指令---kill | killall(终止进程)
kill Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后 ...
- Android下结束进程的方法
转自:http://www.cnblogs.com/crazypebble/archive/2011/04/05/2006213.html 最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这 ...
随机推荐
- mysqldump 备份脚本
#!/bin/bash DUMP=/usr/bin/mysqldump OUT_DIR=/home/mysql LINUX_USER=root DB_NAME=snale DB_USER=root D ...
- java————面向对象概念
面向对象 OO:面向对象 OOP:面向对象编程 OOA:面向对象分析 OOD:面向对象设计 面向对象的特征 继承,封装,多态 什么是对象? 对象是存在的具体实体,具有明确定义的特征和行为. 万物皆对象 ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 福州大学软工1715|W班-启航
新的一学期即将开启,而在仅剩的几天的时间内,我将为接下来的软工实践助教事宜忙碌起来.要学习的东西很多,要关注的东西也很多. 虽然我现在还在茫然阶段,虽然我对<构建之法>还不太熟悉,但是,我 ...
- 听翁恺老师mooc笔记(2)-第一个程序--&运算符
使用devC++写hello world 第一步:文件-新建-源代码.然后输入"输出hello world"程序: 注意:写程序时关闭中文输入法,否则语句输入的分号可能会被识别为错 ...
- 解析与动作联动得SDN数据平面
一种解析与执行联动的SDN可编程数据平面 现有问题和目标 在传统协议处理方式中,各层的协议类型和组合方式固定,使得添加或修改协议很困难(因为需要修改网络设备的解析模式) 基于解析和执行联动结构的可编程 ...
- JAVA_SE基础——66.StringBuffer类 ③
如果需要频繁修改字符串 的内容,建议使用字符串缓冲 类(StringBuffer). StringBuffer 其实就是一个存储字符 的容器. 容器的具备 的行为 常用方法 String 增加 ap ...
- Python 列表嵌套多种实现方式
#coding=utf-8 list=[] for i in range(1,101): list.append(i) # print(list) tempList=[] newList=[] whi ...
- 第一章 IDEA的使用
第一章 IDEA的使用 1.为什么要使用idea 最智能的IDE IDEA相对于eclipse来说最大的优点就是它比eclipse聪明.聪明到什么程度呢?我们先来看几个简单的例子. A.智能提示重 ...
- OAuth是什么?
一.OAuth的概念 1.问题的提出 2.应用场景 3.规范演进 二.OAuth的运行原理 1.参与者 访问私有数据需要用户参与(客户.用户.服务提供者) 访问公共数据不需要用户参与(客户.服务提供者 ...