回溯
不可知论大祭司
Know how to solve every problem that has been solved.
展开
-
leetcode216组合总和 III
class Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> combinationSum3(int k, int n) { List<Integer> path = new ArrayList<>(); backtrack(1, k, n, path);原创 2021-01-05 16:43:11 · 127 阅读 · 0 评论 -
leetcode90 子集 II
codeclass Solution { List<List<Integer>> res = new ArrayList<>(); boolean[] vis; public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); vis = new boolean[nums.length];原创 2021-01-05 16:21:49 · 76 阅读 · 0 评论 -
leetcode78 子集
codeclass Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { List<Integer> path = new ArrayList<>(); backtrack(0, nums, path);原创 2021-01-05 16:13:11 · 133 阅读 · 0 评论 -
leetcode40 组合数总和2
code一开始是这样写的,但是发现有重复的现象,于是把数组排序了一下,在回溯过程中添加判断条件去重。class Solution { List<List<Integer>> res = new ArrayList<>(); boolean vis[]; public List<List<Integer>> combinationSum2(int[] candidates, int target) {原创 2021-01-05 08:52:35 · 173 阅读 · 1 评论 -
leetcode47 全排列
code实际上是在原来的全排列种做了去重的处理,其余的方法大同小异。//leetcode submit region begin(Prohibit modification and deletion)class Solution { boolean[] vis; List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> p原创 2021-01-04 22:51:32 · 100 阅读 · 0 评论 -
全排列的简单实现 java 回溯
介绍从1到n的全排列,我们可以开一个vis数组表示这个数字是否在当前的排列方式种,回溯算法的全排列实际可以看作深度优先搜索codepublic class Test { static boolean[] vis = new boolean[100]; public static void getPermutation(int n, int steps, List<Integer> path, List<List<Integer>> res){原创 2021-01-04 22:25:42 · 285 阅读 · 0 评论