首页 > 其他 > 详细

leetcode532

时间:2017-04-26 18:37:10      阅读:193      评论:0      收藏:0      [点我收藏+]
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

原文:http://www.cnblogs.com/asenyang/p/6769968.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!