给定一个不含重复数字的数组 nums
,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
输入:nums = [1,2,3] 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
package backTrack;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Demo class
*
* @author
* @date 2023-05-23
*/
public class AllResult {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> track = new LinkedList<>();
boolean[] used = new boolean[nums.length];
backTrack(nums, res, track, used);
return res;
}
public static void backTrack(int[] num, List<List<Integer>> res, LinkedList<Integer> track, boolean[] uesd) {
//终止条件,说明到达了叶子,把track全塞进res中
if (track.size() == num.length) {
res.add(new LinkedList<>(track));
return;
}
//水平遍历
for (int i = 0; i < num.length; i++) {
//由于结果不重复,所以要跳过已用元素
if (uesd[i]) {
continue;
}
track.add(num[i]);
uesd[i] = true;
//递归,垂直遍历
backTrack(num, res, track, uesd);
track.removeLast();
uesd[i] = false;
}
}
public static void main(String[] args) {
int[] num = {1, 2, 3};
System.out.println(permute(num));
}
}
给定一个可包含重复数字的序列 nums
,按任意顺序 返回所有不重复的全排列。
输入:nums = [1,1,2] 输出: [[1,1,2], [1,2,1], [2,1,1]]
package backTrack;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* Demo class
*
* @author 张凡
* @date 2023-05-24
*/
public class UniqueAllResult {
public static List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used = new boolean[nums.length];
Arrays.sort(nums);
backTrack(nums, path, used, res);
return res;
}
private static void backTrack(int[] nums, LinkedList<Integer> track, boolean[] used, List<List<Integer>> res) {
if (track.size() == nums.length) {
res.add(new LinkedList(track));
return;
}
for (int i = 0; i < nums.length; i++) {
//当前第i个元素已使用,或当前没使用,但前一个相同,且前一个已使用
if (used[i] || (i > 0 && nums[i] == nums[i - 1]) && used[i - 1]) {
continue;
}
//路径加上第i个值
track.add(nums[i]);
used[i] = true;
//递归
backTrack(nums, track, used, res);
track.removeLast();
used[i] = false;
}
}
public static void main(String[] args) {
int[] nums = {1, 1, 2};
System.out.println(permuteUnique(nums));
}
}
给定两个整数 n
和 k
,返回范围 [1, n]
中所有可能的 k
个数的组合。
你可以按 任何顺序 返回答案
输入:n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
package backTrack;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Demo class
*
* @author 张凡
* @date 2023-05-24
*/
public class Combine {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> combine(int n, int k) {
combineHelper(n, k, 1);
return result;
}
private void combineHelper(int n, int k, int startIndex) {
//终止条件
if (path.size() == k) {
result.add(new LinkedList<>(path));
return;
}
for (int i = startIndex; i <= n; i++) {
path.add(i);
//组合中,不能用重复元素,所以i+1
combineHelper(n, k, i + 1);
path.removeLast();
}
}
public static void main(String[] args) {
System.out.println(new Combine().combine(4, 2));
}
}