问题描述:一串数字,求出其所有的排列组合,例如1,2,3,其组合就有6种
解析:很简单的一道题,运用递归就可以解决,直接上代码:
public class AllGroups {
public static void main(String[] args) {
int[] groups = {1,2,3,4,5};
new AllGroups().cal(groups, 0, groups.length);
}
public void cal(int[]groups, int start, int end){
int _start = start;
if(start == end){
for(int i = 0; i < groups.length; i++){
System.out.print(groups[i]);
}
System.out.println();
return ;
}
while(_start < end){
swap(groups, start, _start);
cal(groups,start+1,end);
swap(groups, start, _start);
_start++;
}
}
public void swap(int[] groups, int start, int _start){
if(start == _start){
return ;
}
int temp = groups[start];
groups[start] = groups[_start];
groups[_start] = temp;
}
}