【问题标题】:C#: recursive search in two different data structuresC#:两种不同数据结构中的递归搜索
【发布时间】:2011-12-15 12:52:46
【问题描述】:

我需要在 C# 中的两种不同数据结构中执行搜索,这里是交易: 我有一个名字(这是一个字符串),我想执行搜索。我有一个名为Exists 的函数,它将返回一个bool 指示它是否存在。

如果存在,我增加名称(只需在字符串末尾添加一个 1),然后我需要再次执行搜索(通过方法exists)以查看是否具有新名称的对象存在。

这将一直持续到有一个未使用的名称,我可以使用它,但是,如果它不存在,现在我应该搜索另一个包含已删除对象的数据结构,如果字符串是找到那里,然后我不得不再次增加名称,并从头开始搜索。 如果没有使用 Exists 方法或所有已删除对象所在的数据结构中没有具有此类名称的对象,这一切都会结束。

我该如何解决这个问题?

我希望我能清楚地表达自己:-)

提前非常感谢!

【问题讨论】:

  • 我看不到任何递归
  • 好的,没有递归我的不好,对不起,也许通过引用将字符串作为参数传递将是一个解决方案?
  • 对我来说不是很清楚。 “被删除的对象” => 什么对象?你之前没谈过删除。
  • @Otiel 可以删除某些具有特定名称的对象,我们必须在现有对象和已删除对象中搜索具有名称的现有项目
  • 你的问题陈述不清楚。

标签: c# data-structures recursion


【解决方案1】:
string BuildNextName(string originalName)
{
  string name = originalName;
  while( Exists(name) || deletedNames.Contains(name))
  {
    name = Increment(name);
  }

  return name;
}

还是我错过了什么?

使用 for 循环:

string BuildNextName(string originalName)
{
  for (string name=originalName; 
       Exists(name) || deletedNames.Contains(name);
       name = Increment(name));

  return name;
}

顺便说一句,我猜你的名字递增算法比简单地加 1 更复杂:name, name1, name2,... 基本上,如果名字不以数字结尾,你附加“1”。如果是这样,则增加该数字。对吧?

【讨论】:

    【解决方案2】:

    如果您为这两种数据结构创建Exists 方法,您可以像这样使用递归进行搜索: 伪代码:

    string resultName;
    void Search(string name)
    {
      if(ExistsInFirstStructure(name)) //name is in first data structure
        Search(name + "1"); //add 1 and try again
      else
        if(ExistsInSecondStructure(name)) //name exists in second data structure
          Search(name + "1"); //perform search again
        else
          resultName = name; //current name wasn't found in first and second data structures - we have result
    }
    

    【讨论】:

      【解决方案3】:

      这个怎么样:

      var listOfExistingNames = new List<string> { "MyName", "MyName1", "MyName3" };
      var listOfDeletedNames = new List<string> { "MyName2", "MyName5" };
      
      int counter = 0;
      string baseToFindFreePlace = "MyName";
      string newName = baseToFindFreePlace;
      
      var allNames = listOfExistingNames.Concat(listOfDeletedNames);
      
      while (allNames.Contains(newName))
      {
          counter++;
          newName = baseToFindFreePlace + counter;
      }
      
      listOfExistingNames.Add(newName);
      

      【讨论】:

        【解决方案4】:

        一个非递归且简单的解决方案可能是这样的(我认为在这种情况下不需要递归)

           //pseudocode
        
            String name;
            bool condition = true;
        
            while(condition)
            {
                if(ExistInFirstDataStructure(name))
                {
                   //increment name
                }
                else
                {
                   if(ExistInDeletedDataStructure(String name))
                   {
                       //increment name
                   }
                   else
                   {
                       condition = false;
                   }
        
                }
            }
        
        
            bool ExistInFirstDataStructure(String name)
            {
        
            }
        
            bool ExistInDeletedDataStructure(String name)
            {
        
            }
        

        【讨论】:

          【解决方案5】:

          为什么要使用循环? (我知道 LINQ 会在后台运行)

          var LastUsedObjectName =
              MyObjects.Select(mo => mo.Name)
                       .Union( MyDeletedObjects.Select(mo => mo.Name))
                       .OrderByDescending(name => /*Function to order by integer part of name*/).First();
          
          // Now add 1 to LastUseObjectName and use that.
          

          【讨论】:

            猜你喜欢
            • 2017-01-18
            • 2010-12-14
            • 1970-01-01
            • 2022-10-14
            • 2015-10-24
            • 2012-10-28
            • 1970-01-01
            • 2016-12-29
            • 1970-01-01
            相关资源
            最近更新 更多