알파벳과 같은 문자열의 순열의 특정 조합을 가져오고 싶습니다. 나는, 내가 5 번째 요소를 필요로하는 경우 나, 목록에서 걸릴 수 있습니다, 잘순열의 특정 조합을 가져 오시겠습니까?
public class PermutationExample {
public static List<String> getPermutation(String input) {
List<String> collection = null;
if (input.length() == 1) {
collection = new ArrayList<String>();
collection.add(input);
return collection;
} else {
collection = getPermutation(input.substring(1));
Character first = input.charAt(0);
List<String> result = new ArrayList<String>();
for (String str : collection) {
for (int i = 0; i < str.length(); i++) {
String item = str.substring(0, i) + first
+ str.substring(i);
result.add(item);
}
String item = str.concat(first.toString());
result.add(item);
}
return result;
}
}
public static void main(String[] args) {
System.out.println(PermutationExample.getPermutation("ABCD"));
}
}
이 코드는 작동하고 나는 모든 조합을 얻을 수 있습니다 : 나를 이해하기 위해, 나는 당신에게 내가 사용하는 코드를 보여 드리겠습니다 그것을받을 수 있습니다. 그러나 그 문자열이 알파벳이라면 ... 작동하지 않았고, 너무 큽니다. 내가해야 할 일은 모든 요소 중 1221 번째와 같은 특정 요소를 얻는 것입니다. 조합?
예, 문자열의 N 번째 순열이 필요하지만 문제를 해결하는 방법을 모르겠습니다. / – Aleksiev