如何以迭代和递归方式打印字符串的所有排列?网络 [英] How to print all permutations of String both iterative and Recursive way? VB.NET

查看:25
本文介绍了如何以迭代和递归方式打印字符串的所有排列?网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我以迭代和递归方式打印字符串的所有排列吗?我更喜欢 VB.NET 或者只是伪代码就可以了.

Can any one help me how to print all permutations of String both iterative and Recursive way? I prefer VB.NET or just pseudo code is fine.

提前致谢.

推荐答案

递归方式如下:

基本情况:1 个字母的排列是一个元素.

Base case: The permutation of 1 letter is one element.

一般情况:一组字母的排列是每个字母的列表,与其他字母的每个排列连接起来.

General case: The permutation of a set of letters is a list each of the letters, concatenated with every permutation of the other letters.

说明:

如果集合只有一个字母,则返回该字母.排列(a) -> a

If the set just have one letter then return that letter. permutation(a) -> a

如果集合有两个字母,则为每个字母返回它和其余字母的排列.

If the set has two letters, for each letter return it and the permutation of the rest of the letters.

排列(ab) ->

a + permutation(b) -> ab

a + permutation(b) -> ab

b + permutation(a) -> ba

b + permutation(a) -> ba

对于集合中的每个字母,返回与其余字母集合的排列组合的字母.

For each letter in set return the letter concatenated with perumation of the rest of the set of letters.

排列(abc)->

a + permutation(bc) --> abc, acb

a + permutation(bc) --> abc, acb

b + permutation(ac) --> bac, bca

b + permutation(ac) --> bac, bca

c + permutation(ab) --> cab, cba

c + permutation(ab) --> cab, cba

这篇关于如何以迭代和递归方式打印字符串的所有排列?网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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