JAVA use while loop easy to understand


  • 0
    G
    public class Solution {
        public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
            
            Map<Integer, List<Integer>> map = new HashMap<>();
            
            List<Integer> res = new ArrayList<>();
            
            int len = pid.size();
            
            
            for(int i=0;i<len;i++){
                int child = pid.get(i);
                int parent = ppid.get(i);
                if(map.containsKey(parent)){
                    map.get(parent).add(child);
                }
                else{
                    List<Integer> tplist = new ArrayList<>();
                    tplist.add(child);
                    map.put(parent,tplist);
                }
                
            }
            
            
            
            List<Integer> tplist1 = new ArrayList<>();
            tplist1.add(kill);
            while(tplist1.size()!=0){
                res.addAll(tplist1);
                List<Integer> tplist2 = new ArrayList<>();
                for(Integer k:tplist1){
                    if(map.get(k)!=null){
                        tplist2.addAll(map.get(k));
                    }
                }
                tplist1 = tplist2;
            }
            return res;
        }
    }
    

Log in to reply
 

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