使用递归连接不同数组中的元素(Connect element in distinct array using recursion)

编程入门 行业动态 更新时间:2024-10-25 12:18:58
使用递归连接不同数组中的元素(Connect element in distinct array using recursion)

如果我有两个数组

A:[A,B] B:[1,2,3]

如何创建字符串列表,如[A_1, A_2, A_3, B_1, B_2, B_3]

阵列的数量不规则,可能还有3个

A:[A,B] B:[1,2,3] C:[w,x,y,z] D:[m,n] E:[p,q,r]

我可以使用递归来解决它吗?

if I have two array

A:[A,B] B:[1,2,3]

how can I create a string List like [A_1, A_2, A_3, B_1, B_2, B_3]

the number of array is not regular, it's maybe have 3 more

A:[A,B] B:[1,2,3] C:[w,x,y,z] D:[m,n] E:[p,q,r]

can I use recursive to solve it?

最满意答案

因此,我们定义一个函数Merge ,它获取stings列表的列表并将它们合并到您想要的可枚举字符串中

static void Main(string[] args) { var a = new[] { "A", "B" }; var b = new[] { "1", "2", "3" }; var c = new[] { "x", "y", "z", "w" }; var result = Merge(a, b, c); foreach (var r in result) { Console.WriteLine(r); } } public static IList<string> Merge(params IEnumerable<string>[] lists) { return Merge((IEnumerable<IEnumerable<string>>) lists); } public static IList<string> Merge(IEnumerable<IEnumerable<string>> lists) { var retval = new List<string>(); var first = lists.FirstOrDefault(); if (first != null) { var result = Merge(lists.Skip(1)); if (result.Count > 0) { foreach (var x in first) { retval.AddRange(result.Select(y => string.Format("{0}_{1}", x, y))); } } else { retval.AddRange(first); } } return retval; }

如果您使用Lists作为输入,我们也可以改进这一点

public static IList<string> Merge(params IList<string>[] lists) { return Merge((IList<IList<string>>) lists); } public static IList<string> Merge(IList<IList<string>> lists, int offset = 0) { if (offset >= lists.Count) return new List<string>(); var current = lists[offset]; if (offset + 1 == lists.Count) // last entry in lists return current; var retval = new List<string>(); var merged = Merge(lists, offset + 1); foreach (var x in current) { retval.AddRange(merged.Select(y => string.Format("{0}_{1}", x, y))); } return retval; }

So, we define a functions Mergethat takes lists of list of stings and merges them into the string enumerable you want

static void Main(string[] args) { var a = new[] { "A", "B" }; var b = new[] { "1", "2", "3" }; var c = new[] { "x", "y", "z", "w" }; var result = Merge(a, b, c); foreach (var r in result) { Console.WriteLine(r); } } public static IList<string> Merge(params IEnumerable<string>[] lists) { return Merge((IEnumerable<IEnumerable<string>>) lists); } public static IList<string> Merge(IEnumerable<IEnumerable<string>> lists) { var retval = new List<string>(); var first = lists.FirstOrDefault(); if (first != null) { var result = Merge(lists.Skip(1)); if (result.Count > 0) { foreach (var x in first) { retval.AddRange(result.Select(y => string.Format("{0}_{1}", x, y))); } } else { retval.AddRange(first); } } return retval; }

we can also improve this, if you use Lists as inputs

public static IList<string> Merge(params IList<string>[] lists) { return Merge((IList<IList<string>>) lists); } public static IList<string> Merge(IList<IList<string>> lists, int offset = 0) { if (offset >= lists.Count) return new List<string>(); var current = lists[offset]; if (offset + 1 == lists.Count) // last entry in lists return current; var retval = new List<string>(); var merged = Merge(lists, offset + 1); foreach (var x in current) { retval.AddRange(merged.Select(y => string.Format("{0}_{1}", x, y))); } return retval; }

更多推荐

本文发布于:2023-04-28 04:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   组中   元素   Connect   recursion

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!