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

Nov*_*ice 7 c# algorithm

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

例如,给定{0,1,2,3,4,7,8,9,1}集我希望使用C#获得{{0,4},{7,9},{11,11}}

C++ @ Solution in C++已经回答了这个问题

Ala*_*Maw 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);
Run Code Online (Sandbox Code Playgroud)

更高效,假设它已分类:

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);
}
Run Code Online (Sandbox Code Playgroud)


luq*_*qui 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;
}
Run Code Online (Sandbox Code Playgroud)

对于更惯用的解决方案,我会返回一个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))"
}
Run Code Online (Sandbox Code Playgroud)