列出不同字符组合的排列

列出不同字符组合的排列,第1张

列出不同字符组合的排列

使用此代码:

public static List<string> GenerateCombinations(char[][] characters){    var combinations = new List<string>();    GenerateCombinations(0, characters, new char[characters.GetLength(0)], combinations);    return combinations;}private static void GenerateCombinations(int level, char[][] characters, char[] current, List<string> combinations){    if (level == characters.GetLength(0))    {        combinations.Add(new string(current));        return;    }    foreach (var character in characters[level])    {        current[level] = character;        GenerateCombinations(level + 1, characters, current, combinations);    }}

使用示例

public static void Main(){    var characters = new[]          {   new[] { 'a', 'b' },   new[] { 'a', 'b' },   new[] { '1', '2' }          };    var combinations = GenerateCombinations(characters);    foreach (var combination in combinations)    {        Console.WriteLine(combination);    }}

输出:

aa1aa2ab1ab2ba1ba2bb1bb2


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4945380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存