Java 如何生成不同大小元素的排列
我输入了一组元素,假设它们是字符串:Java 如何生成不同大小元素的排列,java,permutation,Java,Permutation,我输入了一组元素,假设它们是字符串: String[] elements = {"A","B","C","D"}; 我希望生成大小为1、2和3(直到n-1)元素的所有排列,而不复制任何元素。 因此,输出将完全如下所示: A B C D AB AC AD BC BD CD ABC ABD ACD BCD 当前代码生成了所有排列,但有重复项:( 公共静态字符串[]getAllList(字符串[]元素,int lengthOfList) { //使用上面计算的元素数初始化返回的列表 String[
String[] elements = {"A","B","C","D"};
我希望生成大小为1、2和3(直到n-1)元素的所有排列,而不复制任何元素。
因此,输出将完全如下所示:
A
B
C
D
AB
AC
AD
BC
BD
CD
ABC
ABD
ACD
BCD
当前代码生成了所有排列,但有重复项:(
公共静态字符串[]getAllList(字符串[]元素,int lengthOfList)
{
//使用上面计算的元素数初始化返回的列表
String[]allLists=新字符串[(int)Math.pow(elements.length,lengthOfList)];
//长度为1的列表只是原始元素
如果(lengthOfList==1)
返回元素;
其他的
{
//递归——获取长度为3、长度为2的所有列表,一直到1
String[]allsublist=getAllList(元素,lengthOfList-1);
//将子列表附加到每个元素
int-arrayIndex=0;
for(int i=0;i 对于(inti=1;iQuick and dirty,关键是按照字典顺序构造新字符串。。。
令牌包含A、B、C、D
启动时,结果还包含A、B、C、D
对于运行后显示的输出
结果=perm(标记,结果,1,3)
publicstaticlist-perm(列表标记、列表结果、int-currentLength、int-maxLength){
如果(currentLength==maxLength){
返回结果;
}
List gen=new ArrayList();
for(字符串s:结果){
for(字符串s1:令牌){
如果(s等于(s1)| | s包含(s1)){
继续;
}否则{
字符串温度=s+s1;
char[]ca=temp.toCharArray();
数组。排序(ca);
字符串res=“”;
用于(字符c:ca){
res+=c;
}
如果(发电机包含(res)){
继续;
}
一般增补(res);
}
}
}
如果(gen.size()>0){
addAll(perm(tokens,gen,currentLength+1,maxLength));
}
返回结果;
}
快速而肮脏的关键是按照字典顺序构造新字符串。。。
令牌包含A、B、C、D
启动时,结果还包含A、B、C、D
对于运行后显示的输出
结果=perm(标记,结果,1,3)
publicstaticlist-perm(列表标记、列表结果、int-currentLength、int-maxLength){
如果(currentLength==maxLength){
返回结果;
}
List gen=new ArrayList();
for(字符串s:结果){
for(字符串s1:令牌){
如果(s等于(s1)| | s包含(s1)){
继续;
}否则{
字符串温度=s+s1;
char[]ca=temp.toCharArray();
数组。排序(ca);
字符串res=“”;
用于(字符c:ca){
res+=c;
}
如果(发电机包含(res)){
继续;
}
一般增补(res);
}
}
}
如果(gen.size()>0){
addAll(perm(tokens,gen,currentLength+1,maxLength));
}
返回结果;
}
快速而肮脏的关键是按照字典顺序构造新字符串。。。
令牌包含A、B、C、D
启动时,结果还包含A、B、C、D
对于运行后显示的输出
结果=perm(标记,结果,1,3)
publicstaticlist-perm(列表标记、列表结果、int-currentLength、int-maxLength){
如果(currentLength==maxLength){
返回结果;
}
List gen=new ArrayList();
for(字符串s:结果){
for(字符串s1:令牌){
如果(s等于(s1)| | s包含(s1)){
继续;
}否则{
字符串温度=s+s1;
char[]ca=temp.toCharArray();
数组。排序(ca);
字符串res=“”;
用于(字符c:ca){
res+=c;
}
如果(发电机包含(res)){
继续;
}
一般增补(res);
}
}
}
如果(gen.size()>0){
addAll(perm(tokens,gen,currentLength+1,maxLength));
}
返回结果;
}
快速而肮脏的关键是按照字典顺序构造新字符串。。。
令牌包含A、B、C、D
启动时,结果还包含A、B、C、D
对于运行后显示的输出
结果=perm(标记,结果,1,3)
publicstaticlist-perm(列表标记、列表结果、int-currentLength、int-maxLength){
如果(currentLength==maxLength){
返回结果;
}
List gen=new ArrayList();
for(字符串s:结果){
for(字符串s1:令牌){
如果(s等于(s1)| | s包含(s1)){
继续;
}否则{
字符串温度=s+s1;
char[]ca=temp.toCharArray();
数组。排序(ca);
字符串res=“”;
用于(字符c:ca){
res+=c;
}
如果(发电机包含(res)){
继续;
}
一般增补(res);
}
}
}
如果(gen.size()>0){
addAll(perm(tokens,gen,currentLength+1,maxLength));
}
返回结果;
}
我更改了你的代码位。试试这个
public static String[] getAllLists(String[] elements, int lengthOfList) {
Set<String> result = new HashSet<String>();
//lists of length 1 are just the original elements
if(lengthOfList == 1)
return elements;
else
{
//the recursion--get all lists of length 3, length 2, all the way up to 1
String[] allSublists = getAllLists(elements, lengthOfList - 1);
for(int i = 0; i < elements.length; i++) {
for (int j = i +1; j < allSublists.length; j++) {
//Check duplicate characters
if(allSublists[j].indexOf(elements[i].charAt(0)) == -1) {
String s = elements[i] + allSublists[j];
char[] c = s.toCharArray();
Arrays.sort(c);
result.add(new String(c)); // Add sorted String
}
}
}
return result.toArray(new String[result.size()]);
}
}
公共静态字符串[]GetAllList(字符串
public static List<String> perm(List<String> tokens, List<String> result, int currentLength, int maxLength){
if(currentLength == maxLength){
return result;
}
List<String> gen = new ArrayList<String>();
for(String s : result){
for(String s1 : tokens){
if(s.equals(s1) || s.contains(s1)){
continue;
}else{
String temp = s+s1;
char[] ca = temp.toCharArray();
Arrays.sort(ca);
String res = "";
for(char c : ca){
res+=c;
}
if(gen.contains(res)){
continue;
}
gen.add(res);
}
}
}
if(gen.size() > 0){
result.addAll(perm(tokens,gen,currentLength+1,maxLength));
}
return result;
}
public static String[] getAllLists(String[] elements, int lengthOfList) {
Set<String> result = new HashSet<String>();
//lists of length 1 are just the original elements
if(lengthOfList == 1)
return elements;
else
{
//the recursion--get all lists of length 3, length 2, all the way up to 1
String[] allSublists = getAllLists(elements, lengthOfList - 1);
for(int i = 0; i < elements.length; i++) {
for (int j = i +1; j < allSublists.length; j++) {
//Check duplicate characters
if(allSublists[j].indexOf(elements[i].charAt(0)) == -1) {
String s = elements[i] + allSublists[j];
char[] c = s.toCharArray();
Arrays.sort(c);
result.add(new String(c)); // Add sorted String
}
}
}
return result.toArray(new String[result.size()]);
}
}