使用C#将整数集转换为范围

时间:2010-11-10 18:51:44

标签: c# algorithm

将一组整数转换为一组范围的最常用的方法是什么?

E.g。给定集合{0,1,2,3,4,7,8,9,11}我希望使用C#

这个问题已在C ++ @ Solution in C++

中得到解答

3 个答案:

答案 0 :(得分:11)

这不是很有效,但惯用语:

var nums = new HashSet<int>{0, 1, 2, 3, 4, 7, 8, 9, 11};
IEnumerable<Tuple<int, int>> ranges = Enumerable.Zip(
    nums.Where(n => !nums.Contains(n - 1)),
    nums.Where(n => !nums.Contains(n + 1)),
    Tuple.Create);

更有效率,假设已经分类:

public IEnumerable<Tuple<int, int>> GetContiguousRanges(IEnumerable<int> nums)
{
    int start = nums.First();
    int last = start - 1;
    foreach (int i in nums)
    {
        if (i != last + 1)
        {
            yield return Tuple.Create(start, last);
            start = i;
        }
        last = i;
    }
    yield return Tuple.Create(start, last);
}

答案 1 :(得分:3)

这应该是你提到的帖子非常简单的音译。确保将此代码放在某个类的某个类中,C#代码必须在类中。我假设你对C#不是很熟悉,所以我会做足够的事情来表明它们之间的相同点和不同点,希望你能够处理其余的事情。

struct Range
{
    public Range (int start, int end) { this.start = start; this.end = end; }
    public int start;
    public int end;
}

public static void SetToRanges(Dictionary<int,bool> indices, List<Range> ranges) 
{
    Range r = new Range(int.MinValue, int.MaxValue);
    foreach (int i in indices.Keys)
    {
        // translate rest of code here
    }
    ranges.Add(r);
    return ranges;
}

对于更惯用的解决方案,我会返回IEnumerable<Range>,因此可以同时构建和迭代“列表”:

public static IEnumerable<Range> SetToRanges(Dictionary<int, bool> indices)
{
     // instead of "ranges.Add(r)", use "yield return r".
     // This returns multiple values in order from the function, that can
     // be iterated with "foreach (Range i in SetToRanges(foo))"
}

答案 2 :(得分:-1)

尝试使用K-means聚类来获取范围。您需要指定所需的不同范围。