C#数组子集取 [英] C# Array Subset fetching

查看:1085
本文介绍了C#数组子集取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组,我想,以确定此字节数组的内容的另一大数组作为一个连续的序列中存在。什么是去这样做的最简单的方法是什么?

I have an array of bytes and i want to determine if the contents of this array of bytes exists within another larger array as a continuous sequence. What is the simplest way to go about doing this?

推荐答案

天真的做法是:

public static bool IsSubsetOf(byte[] set, byte[] subset) {
    for(int i = 0; i < set.Length && i + subset.Length <= set.Length; ++i)
        if (set.Skip(i).Take(subset.Length).SequenceEqual(subset))
            return true;
    return false;
}

有关更高效的方法,你可能会考虑更高级的字符串匹配算法,如 KMP

For more efficient approaches, you might consider more advanced string matching algorithms like KMP.

这篇关于C#数组子集取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆