给定一个数组,元素不重复,返回它的所有排列组合。
例如输入:[1,2,3]
返回[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
解题思路:
通过回溯,穷举的方式获取全排列组合
/**
* 给定一个不重复元素的数组,返回它的全排列
*/
public class TestFull {
public List<List<Integer>> arrFullList(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> board = new LinkedList<>();
backtrack(res, board, nums);
return res;
}
public void backtrack(List<List<Integer>> res, LinkedList<Integer> board, int[] nums) {
//满足结束条件
if (board.size() == nums.length) {
res.add(new LinkedList(board));
return;
}
//选择列表
for (int num : nums) {
//排除不符合的选项
if (board.contains(num)) {
continue;
}
//做选择
board.add(num);
//回溯
backtrack(res, board, nums);
//撤销选择
board.removeLast();
}
}
@Test
public void fullTest() {
int[] nums = {1,2,3};
List<List<Integer>> res = arrFullList(nums);
System.out.println(res);
}
}