leetcode609

public class Solution {
    public IList<IList<string>> FindDuplicate(string[] paths) {
        Dictionary<string, List<string>> map = new Dictionary<string, List<string>>();
            foreach (string path in paths)
            {
                string[] tokens = path.Split(' ');
                for (int i = 1; i < tokens.Length; i++)
                {
                    string file = tokens[i].Substring(0, tokens[i].IndexOf('('));

                    var begin = tokens[i].IndexOf('(') + 1;
                    var end = tokens[i].IndexOf(')') - begin;

                    string content = tokens[i].Substring(begin, end);
                    if (!map.ContainsKey(content))
                    {
                        map.Add(content, new List<string>());
                    }
                    map[content].Add(tokens[0] + "/" + file);
                }
            }
            IList<IList<string>> list = new List<IList<string>>();
            var list2 = map.Values.Where(e => e.Count > 1).ToList();
            foreach (var l2 in list2)
            {
                list.Add(l2);
            }

            return list;
    }
}

https://leetcode.com/problems/find-duplicate-file-in-system/#/solutions

原文地址:https://www.cnblogs.com/asenyang/p/6949988.html