如何使用 LINQ 执行合并排序?
假设您有两个 IEnumerbale
对象。我们如何合并它们(在某些情况下,例如合并排序中的合并......)并创建一个唯一的IEnumerable
?我用 Zip
尝试过,但在 Zip 中,两个列表大小应该相等(也许您没有遇到异常,但也许我们丢失了一些数据。)
此外,我通过使用 Enumerable 进行尝试。 Range(...).Select(...) 但我没有得到可接受的结果。
此外,我的问题与使用 Union 或 这个 完全不同,事实上,正如我所说,就像合并排序中的合并一样,我喜欢保留列表顺序(实际上只是想填补第一个列表中的一些空白)。
使用 for 循环很容易处理它,但我看不到任何完整的 linq 方式。
编辑:
Sample input:
lst1 = {5,10,12}
lst2 = {7,9,16,20,25}
result: {5,7,9,10,12,16,20,25}
这可以通过 O(n + m)
中的 for 循环和两个指针来完成,但我正在寻找 O(n+ m)
for循环解决方案:
var lst1 = new List<int> { 5, 10, 12 };
var lst2 = new List<int> { 7, 9, 16, 20, 25 };
var result = new List<int>();
int j = 0;
for (int i = 0; i < lst1.Count; i++)
{
while (j < lst2.Count && lst2[j] < lst1[i])
{
result.Add(lst2[j]);
j++;
}
result.Add(lst1[i]);
}
while (j < lst2.Count)
{
result.Add(lst2[j]);
j++;
}
Console.WriteLine(string.Join(",", result.ToArray()));
Assume that you have two IEnumerbale
objects. How we can merge them (in some condition e.g merge in merge sort ...) and create a unique IEnumerable
? I tried this with Zip
, but in Zip the two list sizes should be equal (maybe you didn't get exception but maybe we have some data lost.)
In addition, I try it by using Enumerable.Range(...).Select(...) but i didn't get an acceptable result.
Furthermore, my question is totally different from using Union or this one, in fact as I said like merge in merge sort I like to preserve lists order (in fact just want to fill some gaps in first list).
It's easy to handle it with for loop, but i can't see any full linq way.
Edit:
Sample input:
lst1 = {5,10,12}
lst2 = {7,9,16,20,25}
result: {5,7,9,10,12,16,20,25}
this can be done with a for loop and two pointer in O(n + m)
but I'm looking for linq solution in O(n+m)
for loop solution:
var lst1 = new List<int> { 5, 10, 12 };
var lst2 = new List<int> { 7, 9, 16, 20, 25 };
var result = new List<int>();
int j = 0;
for (int i = 0; i < lst1.Count; i++)
{
while (j < lst2.Count && lst2[j] < lst1[i])
{
result.Add(lst2[j]);
j++;
}
result.Add(lst1[i]);
}
while (j < lst2.Count)
{
result.Add(lst2[j]);
j++;
}
Console.WriteLine(string.Join(",", result.ToArray()));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ中没有这样的方法。而且我认为不可能结合现有的方法来完全完成您想要的事情(如果是的话,那就太复杂了)。
但自己实现这样的方法并不难:
你可以这样使用它:
There is no such method in LINQ. And I don't think it's possible to combine the existing methods to do exactly what you want (if it was, it would be overly complicated).
But implementing such method yourself isn't that hard:
And you could use it like this:
System.Linq.Enumerable
中没有 Merge 方法。如果你想要一个,你必须编写它(使用 for 循环和yield 语句)。与许多“仅通过 linq”问题一样 - 您应该假设每个 linq 方法都由您自己编写的一些简单的旧 .NET 代码支持。
这是一个未经测试的徒手非通用实现
There is no Merge method in
System.Linq.Enumerable
. If you want one, you have to write it (with a for loop and a yield statement).As with many "just by linq" questions - you should assume that every linq method is backed by some plain old .NET code that you could have written yourself.
Here's an untested freehand non-generic implementation
使用 LINQ 方法
Concat()
http://msdn. microsoft.com/en-us/library/bb302894.aspxUse LINQ method
Concat()
http://msdn.microsoft.com/en-us/library/bb302894.aspx