arraylist 对象的排列
我有一个包含一些对象的数组列表,我必须获得这些对象的排列?我该怎么做? 假设 MyList 是一个包含 4 个对象的数组列表。
ArrayList myList = new ArrayList();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);
所以数组列表计数是 4 所以我想要 4!=24 我想要该对象的 24 种排列。 我怎样才能在 C# 中做到这一点。请帮助我。
谢谢!
I have an arraylist which contains some objects and i have to get permutation of that objects?How can i do that?
Suppose MyList is an arraylist which contains 4 objects.
ArrayList myList = new ArrayList();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);
so arraylist count is 4 so i want 4!=24
I want 24 permutations of that objects.
How can i do That in C#.Please help me.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
斯坦福大学“编程抽象”课程的讲座很好地解释了递归解决方案。
http://www.youtube.com/watch?v=uFJhEPrbycQ#t= 37米25秒
This Stanford lecture from the class "Programming Abstractions" explains a recursive solution really well.
http://www.youtube.com/watch?v=uFJhEPrbycQ#t=37m25s
可能是这样的,虽然没有测试过。
May be like this, not tested though.
这是一篇深入探讨 next_permutation 的 C++ 实现的好文章。是的,它是用 C++ 编写的,但语法没有太大不同,而且解释也足够好。干杯。
Here is a nice article going in depth of C++ implementation of next_permutation. Yes, it's in C++ but the syntax is not much different, and explanation is good enough. Cheers.
你可以用可爱的递归来做到这一点。
基本情况:大小为 1 的数组的排列是数组本身。
递归情况:大小为 n 的数组的排列,每个大小为 (n - 1) 的排列,并在每个可能的位置添加第 n 项。
这有道理吗?
You can do this with lovely lovely recursion.
The base case: the permutation of an array of size 1 being the array itself.
The recursive case: the permutation of the array of size n being, each permutation of size (n - 1) with the nth item added at each possible position.
Does that make sense?
查看此库:使用 C# 泛型进行排列、组合和变体
Take a look into this library: Permutations, Combinations, and Variations using C# Generics