当前位置:网站首页>[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
边栏推荐
- C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
- Longyuan war "epidemic" 2021 network security competition web easyjaba
- Serpentine matrix
- Un réveil de l'application B devrait être rapide
- Components in protective circuit
- How to remove installed elpa package
- Introduction to RT thread kernel (5) -- memory management
- 【UNIAPP】系统热更新实现思路
- File upload bypass summary (upload labs 21 customs clearance tutorial attached)
- 如何进行「小步重构」?
猜你喜欢
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
【科普】热设计基础知识:5G光器件之散热分析
What are the building energy-saving software
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
小程序中实现文章的关注功能
[uniapp] system hot update implementation ideas
【FineBI】使用FineBI制作自定义地图过程
Key review route of probability theory and mathematical statistics examination
Rome chain analysis
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
随机推荐
OWASP top 10 vulnerability Guide (2021)
Machine learning -- neural network
【UNIAPP】系统热更新实现思路
PHP reads the INI file and writes the modified content
How to remove installed elpa package
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
Uncover the seven quirky brain circuits necessary for technology leaders
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
首席信息官如何利用业务分析构建业务价值?
[phantom engine UE] the difference between running and starting, and the analysis of common problems
How should programmers learn mathematics
Learning MVVM notes (1)
Burpsuite grabs app packets
【thingsboard】替换首页logo的方法
Common features of ES6
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
[untitled]
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
Here comes the Lantern Festival red envelope!
Kwai, Tiktok, video number, battle content payment