来自:网易公开课《抽象编程》
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PermuteCode
{
class Program
{
static void Main(string[] args)
{
ListPermutation("ABCDE");
Console.ReadKey();
}
static void ListPermutation(string s)
{
RecPermute("", s);
}
static void RecPermute(string soFar,string rest)
{
if (rest == "")
{
var display = soFar;
Console.WriteLine(display);
return;
}
for (int i = 0; i < rest.Length; i++)
{
string next = soFar + rest[i];
string remaining = rest.Substring(0, i)
+ rest.Substring(i + 1);
RecPermute(next, remaining);
}
}
}
}
本文介绍了一种使用 C# 语言实现字符串全排列的方法。通过递归算法,该程序能够输出输入字符串的所有可能排列组合,适用于算法学习和面试准备。

被折叠的 条评论
为什么被折叠?



