上一页 1 2 3 4 5 6 7 8 9 ··· 31 下一页
摘要: class Solution { private List<String> res = new LinkedList<>(); public List<String> readBinaryWatch(int num) { dfs(num, 0, 0, 1, 1, new LinkedList<Int 阅读全文
posted @ 2020-12-28 22:10 不学无墅_NKer 阅读(66) 评论(0) 推荐(0) 编辑
摘要: ☆☆思路:做完1 和 2之后,直接套模板就完了。刚开始可以把1到9放入数组中,然后回溯搜索。 AC后再去掉数组进行优化。 class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Int 阅读全文
posted @ 2020-12-28 21:14 不学无墅_NKer 阅读(68) 评论(0) 推荐(0) 编辑
摘要: 思路:回溯搜索 + 剪枝。 注意,回溯做选择的方法,不适用于有 “有重复元素的数组”。因此,重点掌握 常规回溯算法的写法。 class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target 阅读全文
posted @ 2020-12-28 17:40 不学无墅_NKer 阅读(77) 评论(0) 推荐(0) 编辑
摘要: ☆☆☆代码: 排序剪枝后效率更高 ~ class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList< 阅读全文
posted @ 2020-12-28 16:51 不学无墅_NKer 阅读(113) 评论(0) 推荐(0) 编辑
摘要: ☆☆☆☆思路:回溯 + 剪枝。 如果使用Set去重,不能AC class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> res = new ArrayList<>(); A 阅读全文
posted @ 2020-12-28 12:30 不学无墅_NKer 阅读(70) 评论(0) 推荐(0) 编辑
摘要: ☆☆☆思路:回溯算法。注意对比区分这三道题:求排列、求组合、求子集。 求组合 和 求子集 方法的对比: 更新res的位置不同:求组合时,res 的更新是当树到达底端时;而求子集,res的更新是树上每一个节点,走过的路径都是子集的一部分。 求组合 和 求排列 方法的对比: 树的形状不同:排列问题的树比 阅读全文
posted @ 2020-12-28 11:46 不学无墅_NKer 阅读(69) 评论(0) 推荐(0) 编辑
摘要: ☆☆☆思路:与排列问题不同,组合问题只需要从当前位置往后搜索。而排列问题每次都需要从头寻找,需要用vis数组记录访问过的元素。 class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer> 阅读全文
posted @ 2020-12-27 22:01 不学无墅_NKer 阅读(64) 评论(0) 推荐(0) 编辑
摘要: 思路1:回溯搜索全排列,使用Set暴力去重。 ☆☆☆思路2:回溯搜索 + 剪枝。 对原数组排序,保证相同的数字都相邻,然后每次填入的数一定是这个数所在重复数集合中「从左往右第一个未被填过的数字」 代码1:回溯搜索 + Set去重 代码1.1 ——交换位置确定数字(耗时:30ms) class Sol 阅读全文
posted @ 2020-12-27 20:59 不学无墅_NKer 阅读(99) 评论(0) 推荐(0) 编辑
摘要: 【举一反三】: 剑指27.字符串的排列 ☆☆回溯算法入门级经典题目,理论讲解及分类习题:回溯算法入门级详解 + 练习(持续更新) 思路1:标记数组 思路2:交换位置。相比思路1,空间复杂度低。 class Solution { public List<List<Integer>> permute(i 阅读全文
posted @ 2020-12-27 11:16 不学无墅_NKer 阅读(64) 评论(0) 推荐(0) 编辑
摘要: ☆☆☆思路1:递归+回溯 思路2:动态规划。 M class Solution { public List<List<String>> partition(String s) { List<List<String>> res = new ArrayList<>(); dfs(s, 0, new Ar 阅读全文
posted @ 2020-12-26 21:05 不学无墅_NKer 阅读(54) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 ··· 31 下一页