import java.util.ArrayList;
import java.util.List;

/**
* 给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
*
* 示例 1:
*
* 输入:nums = [1,2,3]
* 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
*/
public class PrintAllPermutations {

public static void main(String[] args) {
PrintAllPermutations printAllPermutations = new PrintAllPermutations();
int[] nums = new int[]{1, 2, 3, 4};
List<List<Integer>> list = printAllPermutations.getAllList(nums);

for (List<Integer> integers : list) {
System.out.println(integers.toString());
}
}

public List<List<Integer>> getAllList(int[] arr) {
List<List<Integer>> list = new ArrayList<>();
List<Integer> sublist = new ArrayList<>();
getAllList(arr, 0, list, sublist);
return list;
}

private void getAllList(int[] arr, int j, List<List<Integer>> list, List<Integer> sublist) {
if (j == arr.length) {
list.add(new ArrayList<>(sublist));
return;
}

sublist.add(arr[j]);
getAllList(arr, j + 1, list, sublist);
sublist.remove(sublist.size() - 1);
for (int k = j + 1; k < arr.length; k++) {
swap(arr, j, k);
sublist.add(arr[j]);
getAllList(arr, j + 1, list, sublist);
sublist.remove(sublist.size() - 1);
swap(arr, j, k);
}
}

private void swap(int[] arr, int j, int k) {
int temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}

}

/* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */