C#查找字符串的所有排列组合


本文摘自PHP中文网,作者大家讲道理,侵删。

C#查找字符串的所有排列组合

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

// 1. remove first char

// 2. find permutations of the rest of chars

// 3. Attach the first char to each of those permutations.

//     3.1  for each permutation, move firstChar in all indexes to produce even more permutations.

// 4. Return list of possible permutations.

   

public string[] FindPermutations(string word)

        {

            if (word.Length == 2)

            {

                char[] _c = word.ToCharArray();

                string s = new string(new char[] { _c[1], _c[0] });

                return new string[]

                {

                    word,

                    s

                };

            }

   

            List<string> _result = new List<string>();

   

            string[] _subsetPermutations = FindPermutations(word.Substring(1));

            char _firstChar = word[0];

            foreach (string s in _subsetPermutations)

            {

                string _temp = _firstChar.ToString() + s;

                _result.Add(_temp);

                char[] _chars = _temp.ToCharArray();

                for (int i = 0; i < _temp.Length - 1; i++)

                {

                    char t = _chars[i];

                    _chars[i] = _chars[i + 1];

                    _chars[i + 1] = t;

                    string s2 = new string(_chars);

                    _result.Add(s2);

                }

            }

            return _result.ToArray();

        }</string></string>

相关阅读 >>

c#操作mysql的工具类mysqlhelper

c#实现图标锁定到windows任务栏或删除图标

c#分别用前序遍历、中序遍历和后序遍历打印二叉树

c#中登陆账户使用的md5加密算法

常用的c#类

二叉搜索树插入算法c#

c#查找字符串的所有排列组合

c#将文件上传、下载(以二进制流保存到数据库)

c# 四舍五入round函数使用

c#测试代码执行时间的方法

更多相关阅读请进入《代码片段》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...