C# 重复生成序列号
我有以下代码:C# 重复生成序列号,c#,linq,sequence,C#,Linq,Sequence,我有以下代码: var result = AllSequences(1, 10, 3); public static IEnumerable<List<int>> AllSequences(int start, int end, int size) { if (size == 0) return Enumerable.Repeat<List<int>>(new List<int>(), 1); ret
var result = AllSequences(1, 10, 3);
public static IEnumerable<List<int>> AllSequences(int start, int end, int size)
{
if (size == 0)
return Enumerable.Repeat<List<int>>(new List<int>(), 1);
return from i in Enumerable.Range(start, end - size - start + 2)
from seq in AllSequences(i, end, size - 1)
select new List<int> { i }.Concat(seq).ToList();
}
但在开始这个序列之前,希望你是这样的:
2,1,1
2,1,2
2,1,3
2,1,4
.....
生成序列时遇到的问题是,我使用LINQ在这个循环中获得性能我对你的问题有点困惑,但是如果你想让序列从1到10贯穿每个数字,对于所有排列,请尝试类似的方法 我在这里并不提倡使用LINQ,只是尝试对代码进行尽可能少的更改
public static IEnumerable<List<int>> AllSequences(int start, int end, int size)
{
if (size == 0)
return Enumerable.Repeat<List<int>>(new List<int>(), 1);
return from i in Enumerable.Range(start, end)
from seq in AllSequences(start, end, size - 1)
select new List<int> { i }.Concat(seq).ToList();
}
,您如何知道LINQ将获得性能?
public static IEnumerable<List<int>> AllSequences(int start, int end, int size)
{
if (size == 0)
return Enumerable.Repeat<List<int>>(new List<int>(), 1);
return from i in Enumerable.Range(start, end)
from seq in AllSequences(start, end, size - 1)
select new List<int> { i }.Concat(seq).ToList();
}
1, 1, 1
1, 1, 2
... etc
1, 1, 10
1, 2, 1
1, 2, 2
... etc
1, 10, 8
1, 10, 9
1, 10, 10
2, 1, 1
2, 1, 2
.... etc
10, 10, 10