自己实现全排列:I found some old code were good:We should think in a reverse way

来源:互联网 发布:高性能linux 编辑:程序博客网 时间:2024/05/22 03:35

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace zuhe
{
    class Program
    {
        static public void zuhe(char[] c, StringBuilder s, int level)
        {
            for (int i = level; i < c.Length; i++)
            {
                s.Append(c[i]);
                Console.WriteLine(s);
                if (level < c.Length - 1)
                {
                    level++;
                    zuhe(c, s,level);
                }
  s.Length -= 1; //setting lower length to differ the new stringbuider from the old
            }
        }
       
        static void Main(string[] args)
        {
            string me = "abcd";
            char[] my = me.ToCharArray();
            StringBuilder sb = new StringBuilder();
            zuhe(my, sb, 0);
            Console.ReadKey();
        }
    }
}