代码随想录算法训练营第 25 天| 491.递增子序列 46.全排列 47.全排列 II
Leetcode 491.递增子序列
题目链接:https://leetcode.cn/problems/non-decreasing-subsequences/description/
题目描述:
给你一个整数数组 nums
,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。
数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。
示例 1:
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]
提示:
1 <= nums.length <= 15
-100 <= nums[i] <= 100
思路:回溯
1、用数组做hash优化
2、采用hash表
图示:
代码1:用数组做hash优化
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backTracking(nums , 0);
return res;
}
public void backTracking(int[] nums , int startIndex ){
if(path.size() > 1){
res.add(new ArrayList<>(path));
}
int[] used = new int[201];
// 这里使用数组来进行去重操作,题目说数值范围[-100, 100]
for(int i = startIndex ; i < nums.length ; i++){
if(!path.isEmpty() && path.get(path.size() - 1) > nums[i] || (used[nums[i] + 100] == 1)){
continue;
}
used[nums[i] + 100] = 1;
path.add(nums[i]);
backTracking(nums , i + 1);
path.remove(path.size() - 1);
}
}
}
代码2:采用hash表
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backTracking(nums , 0);
return res;
}
// 用hash表
public void backTracking(int[] nums , int startIndex){
if(path.size() > 1){
res.add(new ArrayList<>(path));
}
HashSet<Integer> hs = new HashSet<>();
for(int i = startIndex ; i < nums.length ; i++){
if(!path.isEmpty() && path.get(path.size() - 1) > nums[i] || hs.contains(nums[i])){
continue;
}
hs.add(nums[i]);
path.add(nums[i]);
backTracking(nums , i + 1);
path.remove(path.size() - 1);
}
}
}
总结:
Leetcode 46.全排列
题目链接:https://leetcode.cn/problems/permutations/description/
题目描述:
给定一个不含重复数字的数组 nums
,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
示例 2:
输入:nums = [0,1]
输出:[[0,1],[1,0]]
示例 3:
输入:nums = [1]
输出:[[1]]
提示:
1 <= nums.length <= 6
-10 <= nums[i] <= 10
nums
中的所有整数 互不相同
思路:回溯
1、used标记使用过的元素
2、用path记录是否存在数字,排除已经选择的数字
图示:
代码1:used标记使用过的元素
class Solution {
boolean[] used;
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
if(nums.length == 0){
return res;
}
used = new boolean[nums.length];
backTracking(nums);
return res;
}
public void backTracking(int[] nums){
if(path.size() == nums.length){
res.add(new ArrayList<>(path));
return;
}
for(int i = 0 ; i < nums.length ; i++){
if(used[i]){
// path中已经存在的元素,直接跳过
continue;
}
used[i] = true;
path.add(nums[i]);
backTracking(nums);
path.removeLast();
used[i] = false;
}
}
}
代码2:用path记录是否存在数字,排除已经选择的数字
class Solution {
// 通过path是否存在数字,排除已经选择的数字
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> permute(int[] nums) {
if(nums.length == 0){
return res;
}
backTracking(nums);
return res;
}
public void backTracking(int[] nums){
if(path.size() == nums.length){
res.add(new ArrayList<>(path));
return;
}
for(int i = 0 ; i < nums.length ; i++){
if(path.contains(nums[i])){
// path中已经存在的元素,直接跳过
continue;
}
path.add(nums[i]);
backTracking(nums);
path.removeLast();
}
}
}
总结:
Leetcode 47.全排列 II
题目链接:https://leetcode.cn/problems/permutations-ii/description/
题目描述:
给定一个可包含重复数字的序列 nums
,按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]
示例 2:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
提示:
1 <= nums.length <= 8
-10 <= nums[i] <= 10
思路:
回溯,先排序,后去重,最后出结果
图示:
代码:回溯,先排序,后去重,最后出结果
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used;
public List<List<Integer>> permuteUnique(int[] nums) {
used = new boolean[nums.length];
Arrays.fill(used,false);
Arrays.sort(nums);
backTracking(nums);
return res;
}
public void backTracking(int[] nums){
if(path.size() == nums.length){
res.add(new ArrayList<>(path));
return;
}
for(int i = 0 ; i < nums.length ; i++){
// 树层去除
if( i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false){
continue;
}
if(used[i] == false){
used[i] = true;
path.add(nums[i]);
backTracking(nums);
used[i] = false;
path.removeLast();
}
}
}
}