【问题标题】:Algorithms to find permutation of a string [duplicate]查找字符串排列的算法[重复]
【发布时间】:2015-04-05 07:12:42
【问题描述】:

除了递归技术之外,任何人都可以建议特定的算法来找到给定字符串的不同排列

【问题讨论】:

  • 如果您想要迭代而不是递归,请注意,每个可以写成递归的解决方案都可以以迭代方式编写,正如 Amnon Schohot 在下面的答案中提到的那样。所以我建议你写下递归算法,然后制定迭代解决方案。

标签: string algorithm data-structures permutation


【解决方案1】:

您可以在here 中找到如何使用递归。

然后您可以将此代码移植为使用堆栈,而不是依赖递归使用的线程堆栈。

【讨论】:

    【解决方案2】:

    解决这个问题,如果你不想使用递归,可以通过动态编程,像这样:

    list = originalString.split('')
    index = (0,0)
    list = [""]
    for iteration n in 1 to y:
      index = (index[1], len(list))
      for string s in list.subset(index[0] to end):
        for character c in originalString:
          list.add(s + c)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      相关资源
      最近更新 更多