按顺序生成数字
给定 startIndex、数字计数和最大数字,我如何使用 LinQ 按此顺序生成数字。例如:
Sample Numbers = 1,2,3,4
StartIndex = 1 (i.e it should start from 1)
Sequence number count = 3
Maximum number = 4 (i.e till 4)
Expected result given the above details :
1,2,3
1,3,4
1,2,4
有没有办法使用 linQ 来实现?
How can i generate numbers using LinQ in this sequence given the startIndex,count of numbers and the maximum number.For example:
Sample Numbers = 1,2,3,4
StartIndex = 1 (i.e it should start from 1)
Sequence number count = 3
Maximum number = 4 (i.e till 4)
Expected result given the above details :
1,2,3
1,3,4
1,2,4
Is there a way to do it using linQ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不需要序列的长度是动态的,那么您可以使用:
序列长度硬编码为 3,因为有 3 个可枚举值被连接:x、y、z。
如果要动态连接任意数量的枚举,则可以使用 Eric Lippert 的笛卡尔积 Linq 示例。
您传递一组包含 N 个项目的 k 个序列,它将返回一个长度为 k 的所有组合的集合。
现在,您不希望结果中出现重复的元素。
因此,我在 Eric 的示例中添加了以下内容:
这是最终的解决方案(为清楚起见进行了编辑):
If you didn't need the length of you sequences to be dynamic, then you could use:
The sequence length is hardcoded to 3, because there are 3 enumerables being joined: x, y, z.
If you want to dynamically join an arbitrary number of enumerables, then you can use Eric Lippert's Cartesian Product Linq example.
You pass a set of k sequences of N items, and it will return a set of all combinations of length k.
Now, you don't want repeated elements in your results.
So, I added the following to Eric's example:
Here's the final solution (edited for clarity):
试试这个功能。
//这里 Range(startindex, count)
//生成所有排列
Try this function.
//Here Range(startindex, count)
//generates all permutations
好吧,首先我们把问题说清楚。我假设你的数字是
int
,但这基本上是一个无关紧要的细节,但具体性让思考变得更加简单。你有一个序列
a_0, a_1, a_2, ..., a_N
int 的 >。您有一个满足
1 <= k <= N + 1
的整数k
。您有一个起始索引
start >=0
和一个结束索引end <= N
。您希望长度为
k
的所有子序列a_i0, a_i1, a_i2, ..., a_ik
使得i0 = start
和ik =结束
。那么你的算法就很简单了。您想要生成
{ start + 1, ..., end - 1 }
的大小k - 2
的所有组合。给定这样的组合j1, j2, ..., j(k-1)
,对其进行排序,调用生成的有序序列i1, i2, ..., i(k-1) )
并返回序列a_start, a_i1, a_i2, ..., a_i(k-1), a_end
。现在您已经知道了问题的正式陈述以及解决问题所需的内容,用于生成所述组合的资源有很多。参见Google 搜索:生成组合 C# 或 高德纳第 4A 卷。
Okay, first let's state the problem clearly. I'll assume your numbers are
int
but that's a mostly irrelevant detail but the concreteness makes thinking go more smoYou have a sequence
a_0, a_1, a_2, ..., a_N
ofint
.You have an integer
k
satisfying1 <= k <= N + 1
.You have a starting index
start >=0
and an ending indexend <= N
.You want all subsequences
a_i0, a_i1, a_i2, ..., a_ik
of lengthk
such thati0 = start
andik = end
.Then your algorithm is simple. You want to produce all combinations of size
k - 2
of{ start + 1, ..., end - 1 }
. Given such a combinationj1, j2, ..., j(k-1)
, order it, call the resulting ordered sequencei1, i2, ..., i(k-1)
and return the sequencea_start, a_i1, a_i2, ..., a_i(k-1), a_end
.Now that you know a formal statement of the problem, and what you need to solve it, resources abound for generating said combinations. cf. Google search : Generating combinations C# or Knuth Volume 4A.