Share my Java solution with HashMap


  • 0
    K

    class Solution {
    public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
    Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
    List<Integer> res = new ArrayList<Integer>();
    for(Integer num: pid){
    map.put(num,new ArrayList<Integer>());
    }
    for(int i=0;i<ppid.size();i++){
    if(ppid.get(i) != 0)
    map.get(ppid.get(i)).add(pid.get(i));
    }
    res.add(kill);
    int start = 0;
    int size = res.size();
    while(size!=0){
    for(int i = start ; i < size ; i++){
    res.addAll(map.get(res.get(i)));
    }
    if(res.size() != size){
    start = size;
    size = res.size();
    }else{
    size = 0;
    }
    }
    return res;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.