检查数组是否是带递归的子数组

时间:2012-06-07 09:34:10

标签: c# recursion

我需要编写一个方法,它接收2个数组作为参数,如果第二个数组是第一个数组的子数组,则返回true,否则返回false。我只需要使用没有循环的递归,但我可以使用私有方法。

到目前为止,这是有的:

public static bool findSequence(char[] findIn, char[] toFind)
{
    return compare(findIn, toFind, num);
}

private static int num = 0;

private static bool compare(char[] findIn, char[] toFind, int num)
{
    for (int i = 0; i < findIn.Length; i++)
    {
        if (toFind[i] != findIn[num])
        {
            num++;
            return false;
        }
    }

    num++;
    return true;
}

3 个答案:

答案 0 :(得分:4)

你的方法是错误的,因为你必须使用递归并避免循环,并且你的代码有循环而且没有递归。我认为你应该尝试一下,因为这是非常有用的大脑健身。无论如何,这应该工作(甚至理解这可能是一个很好的练习:-)):

    public static bool FindSequence(char[] findIn, char[] toFind)
    {
        return FindSequence(findIn, toFind, 0, 0);
    }

    private static bool FindSequence(char[] findIn, char[] toFind, int posInFindIn, int posInToFind)
    {
        if (findIn.Length - posInFindIn < toFind.Length - posInToFind)
            return false;
        if (findIn[posInFindIn] == toFind[posInToFind])
        {
            if (posInToFind == toFind.Length - 1)
                return true;
            else
                if (FindSequence(findIn, toFind, posInFindIn + 1, posInToFind + 1))
                    return true;
        }
        return FindSequence(findIn, toFind, posInFindIn + 1, 0);
    }

答案 1 :(得分:1)

以下是用于检查findIn是否在其开头(而不是其长度上的任何位置)包含toFind子阵列的代码的简化版本:

public static bool FindSequence(char[] findIn, char[] toFind)
{
    return findIn.Length >= toFind.Length &&
           FindSequence(findIn, toFind, 0);
}

private static bool FindSequence(char[] findIn, char[] toFind, int pos)
{
    return pos < toFind.Length &&
           findIn[pos] == toFind[pos] &&
           FindSequence(findIn, toFind, pos + 1);
}

答案 2 :(得分:0)

这是一个使用类构造和队列来查找子数组的精确索引的答案。

namespace Alogrithms
{
  public class ArraySearch
  {
    int[] pattern;
    Queue<int> indices = new Queue<int>();  
    int[] source;
    public ArraySearch( int[] pattern, int[] source)
    {
      this.source = source;
      this.pattern = pattern;
    }
    public int[] Pattern
    {
      get { return pattern; }
      private set { pattern = value; }
    }
    public Queue<int> Indices
    {
      get { return indices; }
      private set { indices = value; }
    }
    public int SearchForSubArray(int patternIndexPtr,int sourceIndexPtr, ref int[] source)
    {
      int end  = source.Length;
      if(patternIndexPtr >= pattern.Length || sourceIndexPtr >= end )
        return patternIndexPtr;

      if(pattern[patternIndexPtr] == source[sourceIndexPtr])
      {
        indices.Enqueue(sourceIndexPtr);
        return SearchForSubArray(patternIndexPtr + 1,sourceIndexPtr+1, ref source);
      }
      else
      {
        indices.Clear();
        patternIndexPtr = 0;
        return SearchForSubArray(patternIndexPtr, sourceIndexPtr + 1, ref source);
      }
    }
  }
}

课程用法:

      int [] randomArray = new int[]{9,8,9,6,5,6,4,7,8,5,4,5,6,3,2,1,3,5,6,5,5,9,6,3,4,5,7,6,8,9,6,7,8,9,9,9,8,2,1,3,5,6,5,5,9,6,3,4,5,7,6,8,9,9,9,9,8};
      int[] pattern = new int[] { 6, 7, 8 };
      ArraySearch sequence = new ArraySearch(randomArray,pattern);
      int found = sequence.SearchForSubArray(0, 0, ref randomArray);
      Console.WriteLine("found : " + found);
      Console.WriteLine("Pattern is : " + String.Join(",", sequence.Pattern));
      foreach(int point in sequence.Indices)
      {
        Console.WriteLine(point);
      }
      if ( sequence.Indices.Count == 0 )
      {
        Console.WriteLine("Sequence not found.");
      }
      Console.ReadLine();