我建立了here的代码。我想生成一组的所有排列,例如(取自线程):
Collection: 1, 2, 3
Permutations: {1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
每组都有种可能的排列,但这不是我想要实现的。考虑到以下集合:
这会产生个排列,这是的极端情况。这需要非常长的时间来计算,因为每个零被认为是唯一的。
而不是那样,我只想生成不同的排列。如果我们这样做,那么只有
排列remaining,因为18项是相同的(k)。
现在,我可以运行上述线程中的代码并将结果存储在HashSet中,从而消除了重复的排列。但是,这将是非常低效的。我正在寻找一种能够直接生成排列的算法。
答案 0 :(得分:7)
使用Swap算法查找排列,您可以直接排除产生重复排列的部分。该算法可在互联网上获得,您可以找到更多相关信息。
private static void Main(string[] args)
{
List<int> set = new List<int>
{
20, 4, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
var permutes = Permute(set);
Console.WriteLine(permutes.Count); // outputs 58140
}
private static List<int[]> Permute(List<int> set)
{
List<int[]> result = new List<int[]>();
Action<int> permute = null;
permute = start =>
{
if (start == set.Count)
{
result.Add(set.ToArray());
}
else
{
List<int> swaps = new List<int>();
for (int i = start; i < set.Count; i++)
{
if (swaps.Contains(set[i])) continue; // skip if we already done swap with this item
swaps.Add(set[i]);
Swap(set, start, i);
permute(start + 1);
Swap(set, start, i);
}
}
};
permute(0);
return result;
}
private static void Swap(List<int> set, int index1, int index2)
{
int temp = set[index1];
set[index1] = set[index2];
set[index2] = temp;
}
这是显示交换算法如何工作的图像。
所以你有{A,B,C}, {A,C,B}, {B,A,C}, {B,C,A}, {C,B,A}, {C,A,B}
现在考虑A
和B
是平等的。我用photoshop编辑了图像(对不起,如果我很可怕!)并用B
替换A
。正如您在图片中看到的那样
我发现图像中有重复的内容。如果您跳过它们,您将获得{A,A,C}, {A,C,A}, {C,A,A}
您必须存储已交换的项目,因此如果项目相同且我们已经进行了交换,我们只需跳过以防止欺骗
if (swaps.Contains(set[i])) continue; // skip if we already done swap with this item
swaps.Add(set[i]); // else add it to the list of swaps.
如果您删除此部分进行测试,则此算法将产生重复的排列,控制台将输出n!
。
答案 1 :(得分:2)
这是迄今为止我提出的最佳解决方案。欢迎提出优化建议。它正好返回n!/ k!项目
大约需要1秒来置换{20,4,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }。
private static IEnumerable<int[]> Permute(int[] list)
{
if (list.Length > 1)
{
int n = list[0];
foreach (int[] subPermute in Permute(list.Skip(1).ToArray()))
{
for (int index = 0; index <= subPermute.Length; index++)
{
int[] pre = subPermute.Take(index).ToArray();
int[] post = subPermute.Skip(index).ToArray();
if (post.Contains(n))
continue;
yield return pre.Concat(new[] { n }).Concat(post).ToArray();
}
}
}
else
{
yield return list;
}
}
答案 2 :(得分:0)
让我们试一试:
Knuths
1. Find the largest index j such that a[j] < a[j + 1]. If no
such index exists, the permutation is the last permutation.
{20,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
> > = = = = = = = = = = = = = = = = =
糟糕,没有索引j
,a[j] < a[j + 1]
。但由于我们需要所有不同的排列,我们可以对每个数组进行排序并保证我们从头开始:
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,20}
1. Find the largest index j such that a[j] < a[j + 1]. If no
such index exists, the permutation is the last permutation.
j = 18 since a[18] < a[19]
2. Find the largest index l such that a[j] < a[l]. Since j + 1
is such an index, l is well defined and satisfies j < l.
l = 19 since a[18] < a[19]
3. Swap a[j] with a[l].
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,10}
4. Reverse the sequence from a[j + 1] up to and including the final element a[n].
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,10}
让我们再做几点:
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,10}
yields {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,20}
yields {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,20,0}
yields {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,0,10}
yields {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,10,0}
yields {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,20}
...
正如您所看到的那样,大型元素稳定地(明显地)向左移动,直到:
{10,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
yields {20,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
最终没有重复排列。
答案 3 :(得分:0)
我以前做过这个问题。您只需要先对数组进行排序,然后使用boolean []被访问的数组来标记您访问过的元素。我在Java中使用回溯的解决方案:
public class Solution {
public List<List<Integer>> permuteUnique(int[] num) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
boolean[] visited = new boolean[num.length];
Arrays.sort(num);
helper(result, list, num, visited);
return result;
}
private void helper(List<List<Integer>> result, List<Integer> list,
int[] num, boolean[] visited) {
for (int i = 0; i < num.length; i++) {
if (visited[i]
|| (i > 0 && num[i] == num[i - 1] && !visited[i - 1])) {
continue;
}
list.add(num[i]);
visited[i] = true;
if (list.size() == num.length) {
result.add(new ArrayList<Integer>(list));
} else {
helper(result, list, num, visited);
}
list.remove(list.size() - 1);
visited[i] = false;
}
}
}