首页 > 代码库 > leetcode532

leetcode532

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

leetcode532