码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode582

时间:2017-05-23 11:26:38      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:etc   tar   res   log   solution   target   int   kill   blog   

public class Solution {
    public IList<int> KillProcess(IList<int> pid, IList<int> ppid, int kill)
        {
            if (kill == 0)
            {
                return pid;
            }

            int n = pid.Count;
            Dictionary<int, List<int>> tree = new Dictionary<int, List<int>>();
            for (int i = 0; i < n; i++)
            {
                tree.Add(pid[i], new List<int>());
            }
            for (int i = 0; i < n; i++)
            {
                if (tree.ContainsKey(ppid[i]))
                {
                    var children = tree[ppid[i]];
                    children.Add(pid[i]);
                    if (!tree.ContainsKey(ppid[i]))
                    {
                        tree.Add(ppid[i], children);
                    }
                }
            }

            List<int> result = new List<int>();
            traverse(tree, result, kill);

            return result;
        }

        private void traverse(Dictionary<int, List<int>> tree, List<int> result, int pid)
        {
            result.Add(pid);

            var children = tree[pid];
            foreach (var child in children)
            {
                traverse(tree, result, child);
            }
        }
}

https://leetcode.com/problems/kill-process/#/solutions

leetcode582

标签:etc   tar   res   log   solution   target   int   kill   blog   

原文地址:http://www.cnblogs.com/asenyang/p/6892872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!