public class Solution {
    public int FindPairs(int[] nums, int k) {
        var pair = new Dictionary<string, string>();
            var list = nums.OrderBy(x => x).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = i; j < list.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    else
                    {
                        var dif = Math.Abs(list[i] - list[j]);
                        if (dif > k)
                        {
                            break;
                        }
                        if (dif == k)
                        {
                            if ((!pair.ContainsKey(list[i] + "|" + list[j])) && (!pair.ContainsKey(list[j] + "|" + list[i])))
                            {
                                pair.Add(list[i] + "|" + list[j], i + "|" + j);
                            }
                        }
                    }
                }
            }

            var result = pair.Count();
            return result;
    }
}

https://leetcode.com/problems/k-diff-pairs-in-an-array/#/description

posted on 2017-04-26 18:04  Sempron2800+  阅读(88)  评论(0编辑  收藏  举报