Just nu i M3-nätverket
Jump to content

Permutation i api?


marb

Recommended Posts

Finns det någon smidig metod i java för att permutera (ta ut alla möjliga kombinationer) av en lista?

Ex:

{1,2,3} ska bli

{{1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}}

 

/marb

 

Link to comment
Share on other sites

-Patrik Johansson-

Svar nej men du kan använda denna funktion:

[log]

public static void permutera(Vector left, Vector done) {

if(left.size() == 0) {

System.out.println(done.toString());

}

 

for(int i = 0; i< left.size(); i++) {

Vector temp = new Vector(left);

temp.remove(i);

 

Vector temp2 = new Vector(done);

temp2.add(left.get(i));

 

permutera(temp, temp2);

}

 

}

[/log]

 

//Patrik J

"Insanity: doing the same thing over and over again and expecting different results."
- citat av Albert Einstein

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...