Java数组全排列

 

 

在本文中,我们将看到如何在Java中查找数组的所有排列。


问题1

给定不同整数的数组,请打印该数组的所有排列。

例如:

数组:[10,20,30]

排列是:

[10,20,30] [10,30,20] [20,10,30] [20,30,10] [30,10,20] [30,20,10]

解决方案

我们可以借助递归来解决问题。解释递归非常困难,因此我创建了一个递归树来演示它。

这是相同的代码。

 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
package org.arpit.java2blog;
 
import java.util.ArrayList;
import java.util.List;
 
public class PermutateArray {
 
public static void main(String[] args) {
PermutateArray pa=new PermutateArray();
 
int[] arr= {10, 20, 30};
 
List<List<Integer>> permute = pa.permute(arr);
 
System.out.println("Permuations of array : [10, 20, 30] are:");
System.out.println("=========================================");
for(List<Integer> perm:permute)
{
System.out.println(perm);
}
 
}
public List<List<Integer>> permute(int[] arr) {
List<List<Integer>> list = new ArrayList<>();
permuteHelper(list, new ArrayList<>(), arr);
return list;
}
 
private void permuteHelper(List<List<Integer>> list, List<Integer> resultList, int [] arr){
 
// Base case
if(resultList.size() == arr.length){
list.add(new ArrayList<>(resultList));
}
else{
for(int i = 0; i < arr.length; i++){
 
if(resultList.contains(arr[i]))
{
// If element already exists in the list then skip
continue;
}
// Choose element
resultList.add(arr[i]);
// Explore
permuteHelper(list, resultList, arr);
// Unchoose element
resultList.remove(resultList.size() - 1);
}
}
}
 
}
 

当您在程序上方运行时,将获得以下输出:

数组的排列:[10,20,30]是:
====================================== ====
[10,20,30] [ 10,30,20 ] [ 20,10,30 ] [ 20,30,10 ] [ 30,10,20 ] [ 30,20,10 ]

我已经用下图说明了递归在这里的工作方式。

您需要在新窗口中打开该图并进行缩放。

由于数组中有3个元素,因此每个节点都有3个分支。


问题2

给定整数数组(可以包含重复项),请打印该数组的所有排列。


解决方案

我们也可以使用递归来解决这个问题,但需要照顾重复项。我们将对数组进行排序,因此所有重复项都是连续的。

 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
package org.arpit.java2blog;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class PermuteArrayWithDuplicates {
 
public static void main(String[] args) {
PermuteArrayWithDuplicates pa=new PermuteArrayWithDuplicates();
 
int[] arr= {10, 20, 10};
 
List<List<Integer>> permute = pa.permute(arr);
 
System.out.println("Permuations of array : [10, 20, 10] are:");
System.out.println("=========================================");
for(List<Integer> perm:permute)
{
System.out.println(perm);
}
 
}
public List<List<Integer>> permute(int[] arr) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(arr);
permuteHelper(list, new ArrayList<>(), arr,new boolean[arr.length]);
return list;
}
 
private void permuteHelper(List<List<Integer>> list, List<Integer> resultList, int [] arr, boolean [] used){
 
// Base case
if(resultList.size() == arr.length){
        list.add(new ArrayList<>(resultList));
    } else{
        for(int i = 0; i < arr.length; i++){
            if(used[i] || i > 0 && arr[i] == arr[i-1] && !used[i - 1])
            {  
                 // If element is already used
             continue;
            }
            // choose element
            used[i] = true;
            resultList.add(arr[i]);
            
            // Explore
            permuteHelper(list, resultList, arr, used);
            
            // Unchoose element
            used[i] = false;
            resultList.remove(resultList.size() - 1);
        }
    }
}
 
}
 

 

数组的排列:[10,20,10]是:
======================================= ====
[ 10,10,20 ] [ 10,20,10 ] [ 20,10,10 ]

这就是关于Java中数组的排列的全部内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值