服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - java数组排列组合问题汇总

java数组排列组合问题汇总

2021-03-31 13:31xh15 JAVA教程

这篇文章主要为大家详细汇总了java数组排列组合问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

面试或笔试中,多次遇到以下4个关于排列组合的手撕算法,这里做个笔记,方法日后查阅:

1. 无重复元素的数组,求全排列;
2. 有重复元素的数组,求全排列;
3. 无重复元素的数组,求组合【子集】;
4. 有重复元素的数组,求组合;

以上四类题,可以用统一的模板实现,如下所示:

java" id="highlighter_841768">
?
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 *【组合&&排列】
 *把一个数组里的数组合全部列出,比如1和2列出来为1,2,12,21.
 *这个题目可以扩展成四个:
 *1.无重复数字的数组,求组合
 *2.有重复数字的数组,求组合
 *3.无重复数字的数组,求全排列
 *4.有重复数字的数组,求全排列
 *【通用思路(递归)】:
 *定义一个函数:从候选集candicate中选取一个组合prefix
 *每次从候选集candicate中remove一个数,并加入前缀prefix,打印prefix;
 *再递归从新的candicate中remove下一个数,并加入prefix
 *【对于重复的控制】
 *采用hashset保存prefix,打印之前,判断hashset中是否包含当前生成的prefix,
 *没有则打印,并加入hashset;有则不打印
 *【组合--》排列】
 *只需在打印前加一个判断:若候选集candicate为空,表示遍历完一次,生成一个排列,可打印
 */
 
package xh.offer.practice;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
 
public class listAllGroup{
  public static void main(String[] args) {
    String [] array = {"1","2"};
    String [] repeate = {"1","2","1"};
    listAllGroup test = new listAllGroup();
    System.out.println("**********no repeate list*******");
    test.listAllNoRepeate(Arrays.asList(array),"");//初始化prefix = ""
    System.out.println("**********repeate list*******");
    HashSet<String> noRepeateSet = new HashSet<String>();
    test.listAllRepeate(Arrays.asList(repeate), "", noRepeateSet);
    System.out.println("**************no repeate premutation********************");
    test.premutationNoRepeate(Arrays.asList(array),"");
    System.out.println("*********************repeate premutation**********************");
    HashSet<String> repeateSet = new HashSet<String>();
    test.premutationRepeate(Arrays.asList(repeate),"", repeateSet);
  }
  //无重复的组合
  public void listAllNoRepeate(List<String> candicate,String prefix){
    if(prefix.length() != 0)
      System.out.println(prefix);//结果长度不为0,则打印输出该组合
 
    for(int i = 0;i < candicate.size();i++){
      //将list转换成linklist链表,方便操作
      List<String> tempList = new LinkedList<String>(candicate);
      //templist减少一个数字,并暂存templist中去除的数字
      String tempString = (String) tempList.remove(i);
      //递归
      listAllNoRepeate(tempList,prefix + tempString);
    }
  }
 
  //有重复的组合,加入hashset
  public void listAllRepeate(List<String> candicate,String prefix,HashSet<String> res){
    if(prefix.length() != 0 && !res.contains(prefix)){
      System.out.println(prefix);
      res.add(prefix);
    }
 
    for(int i = 0;i < candicate.size();i++){
      List<String> tempList = new LinkedList<String>(candicate);
      String tempString = tempList.remove(i);
      listAllRepeate(tempList, prefix+tempString, res);//递归
    }
  }
 
  //无重复的全排列,加入判断candicate.size() == 0
  public void premutationNoRepeate(List<String> candicate,String prefix){
    if(candicate.size() == 0){
      System.out.println(prefix);
    }
 
    for(int i = 0;i < candicate.size();i++){
      List<String> tempList = new LinkedList<String>(candicate);
      String tempString = tempList.remove(i);
      premutationNoRepeate(tempList,prefix+tempString);
    }
  }
 
  //有重复的全排列,加入hashset辅助判断输出
  public void premutationRepeate(List<String> candicate,String prefix,HashSet<String> res) {
    if(candicate.size() == 0 && !res.contains(prefix)){
      System.out.println(prefix);
      res.add(prefix);
    }
 
    for(int i = 0;i < candicate.size();i++){
      List<String> tempList = new LinkedList<String>(candicate);
      String tempString = tempList.remove(i);
      premutationRepeate(tempList, prefix+tempString, res);
    
  }
 
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/xionghuixionghui/article/details/78015103

延伸 · 阅读

精彩推荐