Problem
将一个字符组全排序
Input
一个长度小于10的字符串,该字符串由数字1~9组成。字符不会重复出现。
Output
按数字在输入串中出现的次序从小到大的顺序输出该字符组的全排序
Sample Input
132
Sample Output
3
32
321
31
312
2
23
231
21
213
1
13
132
12
123
程序代码如下:
- import java.util.*;
- public class T2 {
- public static void main(String[] args) throws Exception{
- String[] array = new String[]{"3","2","1"};
- listAll(Arrays.asList(array),"");
- }
- public static void listAll(List<String> candidate, String prefix){
- System.out.println(prefix);
- for( int i=0; i < candidate.size(); i++ ) {
- List<String> temp = new ArrayList<String>(candidate);
- listAll(temp, prefix+temp.remove(i));
- }
- }
- }