当前位置:网站首页>[crampon programming] lintcode decoding Encyclopedia - 872 termination process
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
2022-07-05 04:31:00 【BZIClaw】
【 Crampon programming 】LintCode Decoding Encyclopedia —— 872 Terminate the process
author :BZIClaw
872 Terminate the process
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 here
int 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 here
const 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 []int
func (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 := 0
for 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
}
Pay attention to me , Learn more about programming
边栏推荐
- 2022-2028 global and Chinese FPGA prototype system Market Research Report
- 2022-2028 global and Chinese video coding and transcoding Market Research Report
- 可观测|时序数据降采样在Prometheus实践复盘
- 函數(易錯)
- [phantom engine UE] the difference between running and starting, and the analysis of common problems
- Decimal to hexadecimal
- Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
- Official announcement! The third cloud native programming challenge is officially launched!
- Threejs Internet of things, 3D visualization of farms (I)
- User behavior collection platform
猜你喜欢

2022-2028 global and Chinese equipment as a Service Market Research Report

Key review route of probability theory and mathematical statistics examination

MacBook installation postgresql+postgis

What are the building energy-saving software

mxnet导入报各种libcudart*.so、 libcuda*.so找不到

2022-2028 global and Chinese FPGA prototype system Market Research Report

Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)

How should programmers learn mathematics

Matplotlib draws three-dimensional scatter and surface graphs

level17
随机推荐
包 类 包的作用域
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
如何进行「小步重构」?
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
Learning notes 8
[uniapp] system hot update implementation ideas
Serpentine matrix
[phantom engine UE] realize the animation production of mapping tripod deployment
MacBook安装postgreSQL+postgis
web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
自动语音识别(ASR)研究综述
Scope of package class package
Sequence diagram of single sign on Certification Center
Introduction to RT thread kernel (5) -- memory management
Observable time series data downsampling practice in Prometheus
假设检验——《概率论与数理统计》第八章学习笔记
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
【科普】热设计基础知识:5G光器件之散热分析
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
Machine learning -- neural network