c# 排列組合演算法

來源:互聯網
上載者:User

標籤:style   blog   color   os   for   re   

   //排列組合    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;        }    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.