arraylist 对象的排列

发布于 2024-08-18 04:54:53 字数 277 浏览 3 评论 0原文

我有一个包含一些对象的数组列表,我必须获得这些对象的排列?我该怎么做? 假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天涯离梦残月幽梦 2024-08-25 04:54:53

斯坦福大学“编程抽象”课程的讲座很好地解释了递归解决方案。

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

忆离笙 2024-08-25 04:54:53

可能是这样的,虽然没有测试过。

public static IEnumerable<string> permute(string s){
    if (s.Count() > 1)
        return from c in s
               from p in permute(s.Remove(s.IndexOf(c), 1))
               select string.Format("{0}{1}", c, p);
    else
        return new string[] { s };
}

May be like this, not tested though.

public static IEnumerable<string> permute(string s){
    if (s.Count() > 1)
        return from c in s
               from p in permute(s.Remove(s.IndexOf(c), 1))
               select string.Format("{0}{1}", c, p);
    else
        return new string[] { s };
}
傲世九天 2024-08-25 04:54:53

这是一篇深入探讨 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.

等你爱我 2024-08-25 04:54:53

你可以用可爱的递归来做到这一点。

基本情况:大小为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文