首页 > 代码库 > c# 排列组合算法

c# 排列组合算法

   //排列组合    public class FullArrange {        /// <summary>        /// 排列组合        /// </summary>        /// <param name="str">字符串</param>        /// <param name="splitStr">分割的符号,比如";"</param>        /// <returns></returns>        public static List<string> GetArrangeResult(string str,string splitStr) {            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)            {                string[] ca = str.Split(new char[] {Convert.ToChar(splitStr)});                return new List<string>() { ca[0].ToString() + splitStr + ca[1].ToString(), ca[1].ToString() + splitStr + ca[0].ToString() };            }            else            {                string[] array = str.Split(new char[] { Convert.ToChar(splitStr)});                List<string> Splittemp=new List<string>();                List<string> temp = GetArrangeString(array[0].ToString(), array[1].ToString());                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--;                    }                }                foreach (var item in temp)                {                    var  mstr="";                    foreach (var s in item) {                        mstr += s.ToString()+ ";";                    }                                       Splittemp.Add(mstr.TrimEnd(;));                }                return Splittemp;            }        }        public static List<string> GetArrangeString(string parent, string child)        {            List<string> temp = new List<string>();            for (int i = 0; i <= parent.Length; i++)            {                    temp.Add(parent.Insert(i, child.ToString()));            }            return temp;        }    }