题目链接:491. 递增子序列 - 力扣(LeetCode)
作者思考:
这个递增子序列比较像取有序的子集。而且本题也要求不能有相同的递增子序列。这又是子集,又是去重,感觉有点像前两天写过的LeetCode90.子集II 。本题其实是有坑的,在LeetCode90.子集II中我们是通过排序,再用一个数组进行标记数组达到去重的目的。
本题要求是求自增子序列,是不能对原数组进行排序的,排序完会多出好多原本不存在的题解。
递归三部曲
递归函数参数:
本题求子序列,需要一个索引来标明下一次的元素位置,调整下一层的递归起始位置。
void backtarcking(int[] nums, int startindex) {
}
递归函数终止条件:
题目给出要求是自增子序列中至少含有两个元素。其实收集子序列也是收集子集的一种,在收集子集时,我们是把抽象树中的所有节点都进行存放,不进行添加返回值,本题就是多加一个限制条件。
if (path.size() >= 2) {
result.add(new LinkedList<>(path));
//收集子序列也是子集的一种 需要抽象树中所有的节点 故不需要返回
}
单层递归逻辑:
刚才提到过,我们不能对原始数组进行排序操作,故之前的方法用数组标记使用过的元素,显然是不行的。我们可以用Set集合帮助我们进行去重。
在单层递归逻辑中,使用过的元素是不能重复使用的,在整个递归逻辑中也就是path中是可以出现重复的元素。
HashSet<Integer> hashset = new HashSet<>();
for (int i = startindex; i < nums.length; i++) {
if (!path.isEmpty() && nums[i] < path.get(path.size()-1)) {
continue ;
}
if (hashset.contains(nums[i])) {
continue ;
}
path.add(nums[i]);
hashset.add(nums[i]);
backtarcking(nums, i +1);
path.remove(path.size()-1);
}
完整代码:
class Solution {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backtarcking(nums, 0);
return result;
}
void backtarcking(int[] nums, int startindex) {
if (path.size() >= 2) {
result.add(new LinkedList<>(path));
//收集子序列也是子集的一种 需要抽象树中所有的节点 故不需要返回
}
HashSet<Integer> hashset = new HashSet<>();
for (int i = startindex; i < nums.length; i++) {
//子序列应为递增序列
//子序列path的最后一个元素如果大于当前元素
if (!path.isEmpty() && path.getLast() > nums[i]) {
continue ;
}
//单层递归(树的宽度)中的去重
if (hashset.contains(nums[i])) {
continue ;
}
hashset.add(nums[i]);
path.add(nums[i]);
backtarcking(nums, i+1);
path.removeLast();
}
}
}
题目链接:46. 全排列 - 力扣(LeetCode)
作者思考:
递归三部曲
递归函数的参数:
本题是排列问题,是有序的,[1 , 2]和[2 , 1]在结果中是不一样的排序,不需要startindex来记录递归中的位置。
void backtracking(int[] nums) {
}
递归函数的终止条件:
在排列问题中,是数组元素进行排序,故path中元素个数等于nums中元素的个数,也就是path.size() = nums.length
if (path.size() == nums.length) {
result.add(new ArrayList<>(path));
return ;
}
单层递归逻辑:
我们在向path中添加元素的时候,其实每一次递归开始都是从i = 0开始的,在遍历整个数组的时候,那么怎么才能在已经在path中的元素不会被重复添加呢?
可以用集合进行筛选,如果集合中存在这个元素就跳过本次递归。也就是说我们本题甚至都不需要对数组进行排序。
完整代码
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
backtracking(nums);
return result;
}
void backtracking(int[] nums) {
if (path.size() == nums.length) {
result.add(new ArrayList<>(path));
return ;
}
for (int i = 0; i < nums.length; i++) {
if (path.contains(nums[i])) {
continue ;
}
path.add(nums[i]);
backtracking(nums);
path.remove(path.size() -1);
}
}
}
题目链接:47. 全排列 II - 力扣(LeetCode)
作者思考:
本题和上一题很像,本题的给定集合包含了重复元素。处理逻辑就是再对元素进行标记,不重复使用元素。
完整代码:
class Solution {
//存放结果
List<List<Integer>> result = new ArrayList<>();
//暂存结果
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
boolean[] used = new boolean[nums.length];
Arrays.fill(used, false);
Arrays.sort(nums);
backTrack(nums, used);
return result;
}
private void backTrack(int[] nums, boolean[] used) {
if (path.size() == nums.length) {
result.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
// used[i - 1] == true,说明同⼀树⽀nums[i - 1]使⽤过
// used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过
// 如果同⼀树层nums[i - 1]使⽤过则直接跳过
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
continue;
}
//如果同⼀树⽀nums[i]没使⽤过开始处理
if (used[i] == false) {
used[i] = true;//标记同⼀树⽀nums[i]使⽤过,防止同一树枝重复使用
path.add(nums[i]);
backTrack(nums, used);
path.remove(path.size() - 1);//回溯,说明同⼀树层nums[i]使⽤过,防止下一树层重复
used[i] = false;//回溯
}
}
}
}
用set集合去重也可以
class Solution {
List<List<Integer>> result = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
boolean[] used = new boolean[nums.length];
Arrays.sort(nums);
backtracking(nums, used);
return result;
}
void backtracking(int[] nums, boolean[] used) {
if (path.size() == nums.length) {
result.add(new LinkedList<>(path));
return ;
}
HashSet<Integer> hashset = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
//单层递归中 不可以出现重复元素 || //如果当前元素被使用过
if (hashset.contains(nums[i]) || used[i] == true) {
continue ;
}
path.add(nums[i]);
hashset.add(nums[i]);
used[i] = true;//当前元素被使用
backtracking(nums, used);
path.removeLast();
used[i] = false;
}
}
}