public class Solution {
    public string[] FindRestaurant(string[] list1, string[] list2) {
        var dic = new Dictionary<string, int>();
            for (int i = 0; i < list1.Length; i++)
            {
                for (int j = 0; j < list2.Length; j++)
                {
                    if (list1[i] == list2[j])
                    {
                        var restaurant = list1[i];
                        var index = i + j;
                        if (!dic.ContainsKey(restaurant))
                        {
                            dic.Add(restaurant, index);
                        }
                    }
                }
            }

            var minIndex = dic.Min(x => x.Value);
            var list = dic.Where(x => x.Value == minIndex).Select(x => x.Key).ToList();
            return list.ToArray();
    }
}

https://leetcode.com/problems/minimum-index-sum-of-two-lists/#/description

posted on 2017-06-01 08:56  Sempron2800+  阅读(130)  评论(0编辑  收藏  举报