递归函数检查子数组是否 [英] Recursive function to check whether sub-array

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

问题描述

我试图编写一个递归函数来检查第二个数组中是否包含一个数组(我的例子也是子数组)并返回true或false。

I am trying to write a recursive function to check whether one array is contained in a second array (my example is also sub-array) and returns true or false.

例如: [d,e] 包含在 [a,b,c,d,e,f]

我知道如何不进行递归检查(使用循环),但是可以

I know how to check without recursion (using for loops), but can not think of a solution using recursion.

推荐答案

您可以使用哈希将大数组的元素存储在哈希表中,然后检查是否使用递归。子数组中的每个元素是否存在于哈希表中,如果找到了所有元素,那么它将是子数组。

You can use hashing to store elements of large array in hash table and then check whether each element of sub array is there in hash table or not, If all elements found then it will be subarray.

这篇关于递归函数检查子数组是否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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