zoukankan      html  css  js  c++  java
  • C#求所有可能的排列组合

    static System.Collections.Specialized.StringCollection MakeStrings(string[] characters, int finalStringLength)
    {
        int finalLength = finalStringLength; // The length of the final strings.
        System.Collections.Specialized.StringCollection existingStrings;
        System.Collections.Specialized.StringCollection newStrings =
            new System.Collections.Specialized.StringCollection(); // Start with the all one-letter combinations.
     
        newStrings.AddRange(characters);
     
        for (int len = 2; len <= finalLength; len++)
        {
            // Start a new collection of strings based on the existing strings.
            existingStrings = newStrings;
            newStrings = new System.Collections.Specialized.StringCollection();
     
            // Concatenate every string of length (len-1)...
            foreach (string str in existingStrings)
            {
                // ...with every character...
                foreach (string ch in characters)
                {
                    // ...to create every possible string of length len.
                    newStrings.Add(str + ch);
                }
            }
        }
        return newStrings;
    }
  • 相关阅读:

    链表
    队列
    稀疏数组
    SQL——流程控制
    SQL——存储过程与函数
    SOA
    MVC模式
    《一线架构师实践指南》--阅读笔记三
    《一线架构师实践指南》-阅读笔记二
  • 原文地址:https://www.cnblogs.com/rinack/p/4135499.html
Copyright © 2011-2022 走看看