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;
        }
    }

转载于:https://www.cnblogs.com/chaohome/p/3857590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值