【冰爪编程】LintCode 解码大全 —— 872 终止进程
【冰爪编程】LintCode 解码大全 —— 872 终止进程
作者:BZIClaw
872 终止进程
Python:
class Solution:"""@param pid: the process id@param ppid: the parent process id@param kill: a PID you want to kill@return: a list of PIDs of processes that will be killed in the end"""def killProcess(self, pid, ppid, kill):m={}for i,parent in enumerate(ppid):if parent not in m:m[parent] = []m[parent].append(pid[i])ans=[]que=[kill]while que:cur = que.pop(0)ans.append(cur)if cur in m:que +=m[cur]return ans# Write your code here
Java:
public class Solution {/*** @param pid: the process id* @param ppid: the parent process id* @param kill: a PID you want to kill* @return: a list of PIDs of processes that will be killed in the end*/public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {Map<Integer, List<Integer>> map = new HashMap<>();List<Integer> result = new ArrayList<>();for (int i = 0; i < pid.size(); i++) {List<Integer> list = map.getOrDefault(ppid.get(i), new ArrayList<>());list.add(pid.get(i));map.put(ppid.get(i), list);}List<Integer> list = new ArrayList<>();list.add(kill);killChildProcess(map, list, kill);return list;}private void killChildProcess(Map<Integer, List<Integer>> map, List<Integer> list, int kill) {if (map.containsKey(kill)) {for (Integer i : map.get(kill)) {list.add(i);killChildProcess(map, list, i);}}}
}
C++:
class Solution {public:/*** @param pid: the process id* @param ppid: the parent process id* @param kill: a PID you want to kill* @return: a list of PIDs of processes that will be killed in the end*/void dfs(unordered_map<int,vector<int>>& tree,int cur,vector<int>& ret){for(auto& pid:tree[cur]){dfs(tree,pid,ret);}ret.emplace_back(cur);}vector<int> killProcess(vector<int> &pid, vector<int> &ppid, int kill) {// Write your code hereint n=pid.size();vector<int> ret;unordered_map<int,vector<int>> tree;for(int i=0;i<n;++i){tree[ppid[i]].emplace_back(pid[i]);}dfs(tree,kill,ret);return ret;}
};
JavaScript:
export class Solution {/*** killProcess** @param pid: the process id* @param ppid: the parent process id* @param kill: a PID you want to kill* @return: a list of PIDs of processes that will be killed in the end*/killProcess(pid, ppid, kill) {// Write your code hereconst cid = {};for(let i = 0 ; i < ppid.length ; ++i) {const p = ppid[i];const c = pid[i];if(cid[p] === void 0) {cid[p] = [c];}else{cid[p].push(c);}}const ans = [];const dfs = (id) => {ans.push(id);if(cid[id] === void 0) return;if(cid[id]) {for(const c of cid[id]) {dfs(c);}}}dfs(kill);return ans;}}
Go:
/*** @param pid: the process id* @param ppid: the parent process id* @param kill: a PID you want to kill* @return: a list of PIDs of processes that will be killed in the end*/import ("sort")
type Arr []intfunc (l Arr) Len() int { return len(l) }
func (l Arr) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l Arr) Less(i, j int) bool { return l[i] < l[j] }
func killProcess(pid []int, ppid []int, kill int) []int {mapParent := make(map[int][]int)for index, value := range pid {if ppid[index] == 0 {continue}if _, ok := mapParent[ppid[index]]; !ok {mapParent[ppid[index]] = make([]int, 0)}mapParent[ppid[index]] = append(mapParent[ppid[index]], value)}queue := make([]int, 0)queue = append(queue, kill)end := 0for end < len(queue) {for ; end < len(queue); end++ {if _, ok := mapParent[queue[end]]; !ok {continue}for _, next := range mapParent[queue[end]] {queue = append(queue, next)}}}sort.Sort(Arr(queue))return queue
}
关注我,学习更多编程知识
【冰爪编程】LintCode 解码大全 —— 872 终止进程相关推荐
- 【冰爪编程】LintCode 解码大全 —— 1100 奇怪的打印机
[冰爪编程]LintCode 解码大全 -- 1100 奇怪的打印机 作者:BZIClaw 1100 奇怪的打印机 Python: class Solution:"""@ ...
- 【冰爪游戏】MC教程 —— PCL启动器
[冰爪游戏]MC教程 -- PCL启动器 作者:BZIClaw 0. 介绍 最近有几款爆火的盗版 MC 软件,其中的佼佼者即使PCL 下载地址点击这里:PCL启动器下载 1. 安装 本节目的:安装成功 ...
- 【冰爪游戏】MC教程 —— 生存第一天
[冰爪游戏]MC教程 -- 生存第一天 作者:BZIClaw 0. 介绍 准备事情搞完了之后,我们开始一个真正的世界~~ 1. 建立新游戏 我们打开 PCL 并启动,在主菜单中选择 单人游戏 按钮,然 ...
- 《UNIX编程环境》——5.6 zap:使用名字终止进程
本节书摘来自异步社区<UNIX编程环境>一书中的第5章,第5.6节,作者:[美]Brian W. Kernighan , Rob Pike著,更多章节内容可以访问云栖社区"异步社 ...
- 计算机编程英语怎么写,计算机编程英语词汇大全.pdf
计算机编程英语词汇大全 计算机编程英语词汇大全 算法常用术语中英对照 DataStructures基本数据结构 Dictionaries字典 PriorityQueues堆 GraphDataStru ...
- Python编程PTA题解大全——索引
Python编程PTA题解大全--索引 所有的题解都已更新完成,均已通过验证,可能有的算法不是很好,有很大的提升空间,大家可以在下方评论,如果遇到好的算法我也会将其更新到文章中,欢迎大家多多讨论(๑& ...
- Android 编程实用代码大全
转载:Android 编程实用代码大全 http://www.juapk.com/forum.php?mod=viewthread&tid=325&fromuid=263 1. 查看是 ...
- 编程实用工具大全(二)(前后端皆可用,不来看看?)
个人主页:个人主页 系列专栏:精品推荐 由于之前编程实用工具大全深受大家喜欢,所以出了第二期.对于没有看过第一期的小伙伴,可以点击这个链接: 编程实用工具大全(一) 目录 1.零代码工具箱 - ...
- access vba表字段_ACCESS VBA编程(使用技巧大全)[].doc
ACCESS VBA编程(使用技巧大全)[].doc ACCESS VBA编程(使用技巧大全)[1] ACCESS-VBA编程 ACCESS-VBA编程. 控件: 常量控件 acBoundObject ...
最新文章
- ECshop在文章列表页调用文章简介
- apache 修改最大连接数
- Android IOS WebRTC 音视频开发总结(三三)-- Periscope介绍
- 视频分解图片原理;图片合成视频原理
- 第一次参加项目个人工作总结 (转)
- 从零开始编写自己的C#框架(23)——上传组件使用说明
- gcc(g++)头文件搜索路径与库文件搜索路径(转载)
- 哪些场景应用了dsrc通信标准_如何提高量子通信系统的实用性,制约了量子通信系统的应用场景...
- html5文本域禁止拖动,textarea用法 TextArea怎么禁用行滚动条
- Matlab:实现菲涅尔圆孔衍射仿真
- 卷积神经网络(四)——应用
- 管理好团队的七个要点,你做到了几个?
- 关于AWB的肤色问题
- php rm-rf,rm-rf误操作的恢复过程
- IOS界面push跳转后navigationController不显示
- ping检测整个网段地址在线
- 如何进行英文文献检索
- 农夫过河算法最简便的c语言,C语言农夫过河
- 【程序源代码】微信小程序餐饮点餐商城
- MFC 生成中英文版软件对应的工程属性设置