开发者

Finding a sequence in a List

I have a list of integers that I would like to search for a sequence.

For开发者_开发问答 example, if I have a master list: 1, 2, 3, 4, 9, 2, 39, 482, 19283, 19, 23, 1, 29

And I want to find sequence: 1, 2, 3, 4

I would like some easy way to fill a subset list with: 1, 2, 3, 4 + the next five integers in the master list

And then remove the integers in the subset list from the master list so at the end of the operation, my lists would look like this:

Master list: 19, 23, 1, 29

Subset list: 1, 2, 3, 4, 9, 2, 39, 482, 19283

Hope that makes sense. I'm guessing maybe LINQ would be good for something like this, but I've never used it before. Can anyone help?


You could start with writing a function IndexOfSublist that can find a sublist inside a list. This has been asked many times before.

Assuming you have implemented IndexOfSublist, use it to find the first element of your result, then you could use GetRange to select the elements that you want to keep and then RemoveRange to remove the elements from the original list.

List<int> l = new List<int> { 1, 1, 2, 3, 4, 9, 2, 39, 482, 19283, 19, 23, 1, 29 };
List<int> needle = new List<int> { 1, 2, 3, 4 };

int i = indexOfSubList(l, needle);
if (i != -1)
{
    // TODO: What should we do if there are fewer than four
    // elements left after the needle? Currently we just throw.
    List<int> result = l.GetRange(i, needle.Count + 4);
    l.RemoveRange(i, result.Count);
    // TODO: Do something with the result;
}


HINT

you can try something like this along with an iterator:

List<int> n = new List<int>() { 1, 2, 3, 4, 9, 2, 39, 482, 19283, 19, 23, 1, 29 };
List<int> toFind = new List<int>() { 1, 2, 3, 4 };

if (n.Skip(indextostart).Take(4).ToList()==toFind)
{
    n.RemoveRange(indextostart,4);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜