问题:给定一个数组,以数组的方式输出其所有的排序。
可以看作时将一个元素向n-1个元素的数组里面插入的所有方法,有n中插入方式。这样就可以将问题简化为求n-1个数组元素的所有顺序,以此类推
//设置一个列表用来存储结果
private static ArrayList<List<Integer>> resp = new ArrayList<>();
public static void getAllArray(List<Integer> arr,List<Integer> temp){
//当将所有的数据取出来,就输出
if(arr.size() < 1){
resp.add(temp);
return ;
}
for (int i = 0;i < arr.size();i++){
temp.add(arr.remove(0));
getAllArray(new ArrayList<>(arr),new ArrayList<>(temp));
arr.add(temp.remove(temp.size()-1));
}
}