Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取序列的所有子序列_C#_Linq - Fatal编程技术网

C# 获取序列的所有子序列

C# 获取序列的所有子序列,c#,linq,C#,Linq,我有一个数组,我需要所有可能的子数组段或子序列,空的除外。这不是幂集,因为每个子阵列只有在输入阵列中连续的元素 例如,对于input new int[]{1,2,3},输出为: new int[]{ new int[]{1}, new int[]{1,2}, new int[]{1,2,3}, new int[]{2}, new int[]{2,3}, new int[]{3} } 注意,{1,3}不存在,因为我不需要幂集的所有子集,只需要所有子序列 我

我有一个数组,我需要所有可能的子数组段或子序列,空的除外。这不是幂集,因为每个子阵列只有在输入阵列中连续的元素

例如,对于input new int[]{1,2,3},输出为:

 new int[]{
   new int[]{1},
   new int[]{1,2},
   new int[]{1,2,3},
   new int[]{2},
   new int[]{2,3},
   new int[]{3}
 }
注意,{1,3}不存在,因为我不需要幂集的所有子集,只需要所有子序列

我更喜欢使用单个LINQ语句的解决方案。

假设您的源是列表,如果不是,请转换为列表,然后您可以执行以下操作:

var srcl = src.ToList();
var ans = Enumerable.Range(0, srcl.Count).SelectMany(start => Enumerable.Range(1, srcl.Count-start).Select(count => srcl.GetRange(start, count)));
使用自然ArraySegment扩展:

public static class ArrayExt {
    public static IEnumerable<T> Segment<T>(this T[] src, int start, int count) => new ArraySegment<T>(src, start, count);
}

但列表通常是首选的。

尽管NetMage的解决方案是正确的,但我最终编写了自己的扩展方法,使用数组。复制以提高性能:

/// <summary>
/// Get all subsequences of the given sequence.
/// {1,2,3}=>{{1,2},{1,2,3},{2,3}}
/// </summary>
public static T[][] GetAllSubsequences<T>(this IEnumerable<T> collection)
{
    var list = (collection as T[]) ?? collection.ToArray();
    return list.SelectMany((x, i) => list.Skip(i).Select((z, j) =>
          {
              var arr = new T[j + 1];
              Array.Copy(list, i, arr, 0, j + 1);
              return arr;
          })).ToArray();
}

-意识到这是一个可能的重复,投票反对的人关心给出一个解释为什么?我想这是因为你的问题读起来像我有一个要求,我想有人写代码me@jdphenix这个问题是得到所有组合的幂集。我想要的是所有的子序列。在问题中提出正确的C类型总是有用的。{1,2,3}不是有效的C对象。GetRange是否确实存在性能问题?除了错误检查,List.GetRange实际上在内部使用Array.Copy。
/// <summary>
/// Get all subsequences of the given sequence.
/// {1,2,3}=>{{1,2},{1,2,3},{2,3}}
/// </summary>
public static T[][] GetAllSubsequences<T>(this IEnumerable<T> collection)
{
    var list = (collection as T[]) ?? collection.ToArray();
    return list.SelectMany((x, i) => list.Skip(i).Select((z, j) =>
          {
              var arr = new T[j + 1];
              Array.Copy(list, i, arr, 0, j + 1);
              return arr;
          })).ToArray();
}