欢迎光临小站,愿能为您提供帮助与启发,热爱分享、享受分享、乐于分享,让我们携手共同进步。
如:传入“123”传出:123、321、132、213、231、312
//调用类和方法
List<string> result= FullArrange.GetArrangeResult(“abc”);
//一个新类
public class FullArrange
{
public static List<string> GetArrangeResult(string str)
{
str = str.Trim();
if (string.IsNullOrEmpty(str))
{
return new List<string>();
}
else if (str.Length == 1)
{
return new List<string> { str };
}
else if (str.Length == 2)
{
char[] ca = str.ToArray();
return new List<string>() { ca[0].ToString() + ca[1] you can check here.ToString(), ca[1].ToString() + ca[0].ToString() };
}
else
{
char[] array = str.ToCharArray();
List<string> temp = GetArrangeString(array[0].ToString(), array[1]);
for (int i = 2; i < array.Length; i++)
{
int count = temp.Count;
for (int j = 0; j < count; j++)
{
temp.AddRange(GetArrangeString(temp[j], array[i]));
temp.Remove(temp[j]);
j–;
count–;
}
}
return temp;
}
}
private static List<string> GetArrangeString(string parent, char child)
{
List<string> temp = new List<string>();
for (int i = 0; i <= parent.Length; i++)
{
temp.Add(parent.Insert(i, child.ToString()));
}
return temp;
}
}