电子商务网站建设课后习题答案成都服务器idc托管
news/
2025/9/23 11:45:30/
文章来源:
电子商务网站建设课后习题答案,成都服务器idc托管,关键词竞价排名,网站导航样式文章目录 DFS排列组合问题排列组合问题的标准模板排列LeetCode46全排列题目描述代码 LeetCode47全排列Ⅱ题目描述代码 组合LeetCode77组合题目描述代码 LeetCode39组合总和题目描述代码 LeetCode40组合总和Ⅱ题目描述代码 LeetCode216组合总和Ⅲ题目描述代码 DFS排列组合问题
… 文章目录 DFS排列组合问题排列组合问题的标准模板排列LeetCode46全排列题目描述代码 LeetCode47全排列Ⅱ题目描述代码 组合LeetCode77组合题目描述代码 LeetCode39组合总和题目描述代码 LeetCode40组合总和Ⅱ题目描述代码 LeetCode216组合总和Ⅲ题目描述代码 DFS排列组合问题
排列组合问题的标准模板
public void dfs(int[] nums,boolean[] isViseted,…………){if(搜索终止的条件){将找到的结果放入结果集中}// 若为组合题则从上一次访问元素或上一次访问元素的下一位开始遍历for(int i 0;inums.length;i){ if(isViseted[i] false){res.add(nums[i]); // 如果元素未被访问过则将元素放入结果中isViseted[i] true;dfs(nums,isViseted,res,res1); res.remove((Integer) nums[i]); //递归结束后恢复成原先的样子isViseted[i] false;}}
}排列与组合最大的区别就是for循环中开始的元素。如果是排列题结果是有序的我们需要从0开始遍历如果是组合题结果是无序的我们需要从i或i1开始遍历。下面将举例说明。
排列
LeetCode46全排列
题目描述
给定一个不含重复数字的数组 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] 10nums 中的所有整数 互不相同
代码
class Solution {// 直接套用上述模板其中res为当前搜寻的结果res1为需要返回的结果集public void dfs(int[] nums,boolean[] isViseted,ListInteger res,ListListInteger res1){if(res.size() nums.length){res1.add(new ArrayList(res));}for(int i 0;inums.length;i){if(isViseted[i] false){res.add(nums[i]);isViseted[i] true;dfs(nums,isViseted,res,res1);res.remove((Integer) nums[i]);isViseted[i] false;}}}public ListListInteger permute(int[] nums) {ListListInteger res new ArrayList();int len nums.length;dfs(nums,new boolean[len],new ArrayListInteger(),res);return res;}
}LeetCode47全排列Ⅱ
题目描述
给定一个可包含重复数字的序列 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 {public static void rm(ListInteger l,Integer num){for(int il.size()-1;i0;i--){if(l.get(i) num){l.remove(i);return;}}}public static void dfs(int[] nums,boolean[] isVisited,ListInteger ele,ListListInteger res){int len nums.length;if(ele.size() len){for(ListInteger l : res){if(l.equals(ele)){return;}}res.add(new ArrayList(ele));return;}for(int i 0;ilen;i){if(!isVisited[i]){ele.add(nums[i]);isVisited[i] true;dfs(nums,isVisited,ele,res);isVisited[i] false;Integer tem nums[i];rm(ele,tem);}}}public ListListInteger permuteUnique(int[] nums) {ListListInteger res new ArrayList();dfs(nums,new boolean[nums.length],new ArrayList(),res);return res;}
}本题与上一题的区别在于结果中会出现重复数字因此我们在恢复元素的时候就不能直接调用remove方法因为remove方法删除的是从前往后找到的一个元素但我们需要删除的元素在列表尾部需要自己写一个rm方法从后向前找元素并删除。
组合
LeetCode77组合
题目描述
给定两个整数 n 和 k返回范围 [1, n] 中所有可能的 k 个数的组合。
你可以按 任何顺序 返回答案。
示例 1
输入n 4, k 2
输出
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],
]示例 2
输入n 1, k 1
输出[[1]]提示
1 n 201 k n
代码
class Solution {public static void dfs(int n,int k,int begin,ListInteger ele,ListListInteger res){if(ele.size() k){res.add(new ArrayList(ele));return;}for(int ibegin;in;i){ele.add(i);dfs(n,k,i1,ele,res);ele.remove((Integer) i);}}public ListListInteger combine(int n, int k) {ListListInteger res new ArrayList();dfs(n,k,1,new ArrayList(),res);return res;}
}与排列问题的不同之处就在于我们把for循环中的起始位置改成了begin上一次访问元素的下一位
LeetCode39组合总和
题目描述
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target 找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同则两种组合是不同的。
对于给定的输入保证和为 target 的不同组合数少于 150 个。
示例 1
输入candidates [2,3,6,7], target 7
输出[[2,2,3],[7]]
解释
2 和 3 可以形成一组候选2 2 3 7 。注意 2 可以使用多次。
7 也是一个候选 7 7 。
仅有这两种组合。示例 2
输入: candidates [2,3,5], target 8
输出: [[2,2,2,2],[2,3,3],[3,5]]示例 3
输入: candidates [2], target 1
输出: []提示
1 candidates.length 302 candidates[i] 40candidates 的所有元素 互不相同1 target 40
代码
class Solution {public static void dfs(int[] candidates,int target,int count,int begin,ListInteger ele,ListListInteger res){if(count target){return;}else if(count target){res.add(new ArrayList(ele));return;}for(int i begin;icandidates.length;i){ele.add(candidates[i]);dfs(candidates,target,count candidates[i],i,ele,res);ele.remove((Integer) candidates[i]);}}public ListListInteger combinationSum(int[] candidates, int target) {ListListInteger res new ArrayList();dfs(candidates,target,0,0,new ArrayList(),res);return res;}
}本题需要我们在递归的同时计算当前的和只需多加个count参数来记录总和。
LeetCode40组合总和Ⅱ
题目描述
给定一个候选人编号的集合 candidates 和一个目标数 target 找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
**注意**解集不能包含重复的组合。
示例 1:
输入: candidates [10,1,2,7,6,1,5], target 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]示例 2:
输入: candidates [2,5,2,1,2], target 5,
输出:
[
[1,2,2],
[5]
]提示:
1 candidates.length 1001 candidates[i] 501 target 30
代码
class Solution {public static void dfs(int[] candidates,int target,int count,int begin,ListInteger ele,ListListInteger res){if(count target){return;}else if(count target){for(ListInteger l : res){if(l.equals(ele)){return;}}res.add(new ArrayList(ele));return;}for(int i begin;icandidates.length;i){ele.add(candidates[i]);dfs(candidates,target,count candidates[i],i1,ele,res);int rem candidates[i];ele.remove((Integer) candidates[i]);while(i candidates.length-1 rem candidates[i1]){i;}}}public ListListInteger combinationSum2(int[] candidates, int target) {Arrays.sort(candidates);ListListInteger res new ArrayList();dfs(candidates,target,0,0,new ArrayList(),res);return res;}
}本题与上一题的区别在于数组里含有重复数字我们需要先将数组进行排序将重复数字放在一起这样我们就不会将结果相同但顺序不同的数组当成不同的结果放入结果集。
whie循环在递归结束后判断下一个要递归的元素是否与当前元素相同如果相同则可以直接跳过。
LeetCode216组合总和Ⅲ
题目描述
找出所有相加之和为 n 的 k 个数的组合且满足下列条件
只使用数字1到9每个数字 最多使用一次
返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次组合可以以任何顺序返回。
示例 1:
输入: k 3, n 7
输出: [[1,2,4]]
解释:
1 2 4 7
没有其他符合的组合了。示例 2:
输入: k 3, n 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解释:
1 2 6 9
1 3 5 9
2 3 4 9
没有其他符合的组合了。示例 3:
输入: k 4, n 1
输出: []
解释: 不存在有效的组合。
在[1,9]范围内使用4个不同的数字我们可以得到的最小和是1234 10因为10 1没有有效的组合。提示:
2 k 91 n 60
代码
class Solution {public static void dfs(int k,int n,int begin,int count, ListInteger target,ListListInteger res){if(count n){return;}if(target.size() k){if(count n){res.add(new ArrayListInteger(target));}return;}for(int ibegin;i9;i){target.add(i);dfs(k,n,i1,counti,target,res);target.remove((Integer) i);}}public ListListInteger combinationSum3(int k, int n) {ListListInteger res new ArrayList();dfs(k,n,1,0,new ArrayList(),res);return res;}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/912425.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!