Bonsoir,
Je souhaite construire, les ensembles suivant avec un algo récursif:
avec pour entrer: String str = "12 13 14 23 24 34";
Toutes conbinaisons à 3 elts sans répétition par exple (12 13 14); (13 14 23)...
et de 4 elts toutes les combinaison sans repetition d'ensembe: par exple(12 13 14 23), (14 23 24 34)
voici un code, la boucle while tourne de maniere infini, alors qu'il n 'a plus de next.
Merci pour votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public static LinkedList<String> permutationRec(String s, int indice, int n){ LinkedList<String> permutation = new LinkedList<String>(); String str = s.substring(indice, s.length()); if(str.length() == n){ permutation.add(str); } else{ LinkedList<String> sub = permutationRec(s, indice+n, n); Iterator<String> it = sub.iterator(); int i = 0; ???? while(it.hasNext()) { String prefixe = sub.get(i); String suffixe = ""; String temp = prefixe + str.substring(0,n) + suffixe; permutation.add(temp); i ++; } } return permutation; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 public static void main(String[] args) { String str = "121214232434"; LinkedList<String> res = permutationRec(str, 0,2); Iterator<String> it = res.iterator(); while(it.hasNext()) { System.out.println(it.next()); } }
Partager