力扣 491.递增子序列
class Solution {
public List<List<Integer>> result = new LinkedList<>();
public List<Integer> path = new ArrayList<>();
public void backTracking(int[] nums, int startId) {
if(path.size() >= 2) {
result.add(new ArrayList<>(path));
}
boolean[] used = new boolean[205];
for(int i = startId; i < nums.length; i++) {
if(!path.isEmpty() && nums[i] < path.get(path.size() - 1)) {
continue;
}
if(used[nums[i] + 100]) {
continue;
}
path.add(nums[i]);
used[nums[i] + 100] = true;
backTracking(nums, i + 1);
path.removeLast();
}
}
public List<List<Integer>> findSubsequences(int[] nums) {
backTracking(nums, 0);
return result;
}
}
力扣 46.全排列
class Solution {
public List<List<Integer>> result = new LinkedList<>();
public List<Integer> path = new ArrayList<>();
public void backTracking(int[] nums, boolean[] used) {
if(path.size() == nums.length) {
result.add(new ArrayList<>(path));
}
for(int i = 0; i < nums.length; i++) {
if(used[nums[i] + 10]) {
continue;
}
path.add(nums[i]);
used[nums[i] + 10] = true;
backTracking(nums, used);
path.removeLast();
used[nums[i] + 10] = false;
}
}
public List<List<Integer>> permute(int[] nums) {
boolean[] used = new boolean[25];
backTracking(nums, used);
return result;
}
}
力扣 47.全排列 II
class Solution {
public List<List<Integer>> result = new LinkedList<>();
public List<Integer> path = new ArrayList<>();
public void backTracking(int[] nums, boolean[] usedId) {
if(path.size() == nums.length) {
result.add(new ArrayList<>(path));
}
for(int i = 0; i < nums.length; i++) {
if(usedId[i] == true) {
continue;
}
if(i > 0 && nums[i] == nums[i - 1] && !usedId[i - 1]) {
continue;
}
path.add(nums[i]);
usedId[i] = true;
backTracking(nums, usedId);
path.removeLast();
usedId[i] = false;
}
}
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
boolean[] usedId = new boolean[10];
backTracking(nums, usedId);
return result;
}
}