码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode532

时间:2017-04-26 19:13:44      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:bre   int   ++   pairs   result   http   code   ==   lan   

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

标签:bre   int   ++   pairs   result   http   code   ==   lan   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!