Sharing My AC Solution


  • 0
    Y
    public class Solution {
    
    
        public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < pid.size(); i++) {
                map.put(pid.get(i), ppid.get(i));
            }
            List<Integer> result = new ArrayList<>();
            for (int i = 0; i < pid.size(); i++) {
                if (isFuHe(pid.get(i), kill,map)) {
                    result.add(pid.get(i));
                }
            }
            return result;
        }
    
        private boolean isFuHe(Integer abc, Integer kill,Map<Integer,Integer> map) {
            while (abc != 0) {
                if (abc.equals(kill)) {
                    return true;
                }
                abc = map.get(abc);
            }
            return false;
        }
    }
    

Log in to reply
 

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