
回溯
wjy_1126
这个作者很懒,什么都没留下…
展开
-
矩阵中的路径-回溯
题目给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。示例输入:board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED”输出:true.原创 2022-02-13 10:23:23 · 192 阅读 · 0 评论 -
递增子序列-回溯
题目给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。示例输入: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]]代码class Solution { List<List<Integer>> res = new ArrayList<>(); .原创 2022-02-13 10:05:12 · 99 阅读 · 0 评论 -
子集II-回溯
题目给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。示例输入:nums = [1,2,2]输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]代码class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> cur = new ArrayList<>().原创 2022-02-12 16:52:19 · 66 阅读 · 0 评论 -
组合-回溯
题目给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。示例输入:n = 1, k = 1输出:[[1]]代码class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> cur = new ArrayList<>(); public void dfs(int n, int .原创 2022-02-12 16:29:17 · 79 阅读 · 0 评论 -
组合总和III-回溯
题目找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。示例输入: k = 3, n = 9输出: [[1,2,6], [1,3,5], [2,3,4]]代码class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> cur = new ArrayList&l.原创 2022-02-12 16:24:22 · 159 阅读 · 0 评论 -
子集-回溯
题目给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。示例输入:nums = [1,2,3]输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]代码class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> cur = new ArrayList.原创 2022-02-12 16:02:27 · 71 阅读 · 0 评论 -
组合总和II-回溯
题目给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的每个数字在每个组合中只能使用 一次 。实例输入: candidates = [2,5,2,1,2], target = 5,输出:[[1,2,2],[5]]代码class Solution { List<List<Integer>> res = new Ar.原创 2022-02-12 15:44:28 · 98 阅读 · 0 评论 -
全排列II-回溯
题目给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。示例输入:nums = [1,2,3]输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]代码class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> cur = new ArrayLis.原创 2022-02-12 11:39:51 · 79 阅读 · 0 评论 -
电话号码的字母组合-回溯
题目给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。示例输入:digits = “23”输出:[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]代码class Solution { List<String> res = new ArrayList<>(); StringBuilder cur = new StringBuilder(); //.原创 2022-02-12 11:05:53 · 127 阅读 · 0 评论 -
24点游戏-回溯
题目给定一个长度为4的整数数组 cards 。你有 4 张卡片,每张卡片上都包含一个范围在 [1,9] 的数字。您应该使用运算符 [’+’, ‘-’, ‘*’, ‘/’] 和括号 ‘(’ 和 ‘)’ 将这些卡片上的数字排列成数学表达式,以获得值24。题目链接示例输入: cards = [4, 1, 8, 7]输出: true代码class Solution { // 把选的两个数删除,并且添加新的结果 public List<Double> get(L.原创 2022-02-12 10:42:26 · 2559 阅读 · 0 评论 -
组合总和-回溯
题目给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。题目链接示例输入:candidates = [2,3,6,7], target = 7输出:[[2,2,3],[7]]代码cl.原创 2022-02-12 10:09:38 · 61 阅读 · 0 评论 -
N皇后-回溯
题目皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。示例输入:n = 4输出:[[".Q…","…Q",“Q…”,"…Q."],["…Q.",“Q…”,"…Q",".Q…"]]算法注释代码class Solution { List<List<String>> res = new ArrayList<>(); char[][] cur; // 标记每一列是否用过.原创 2022-02-12 09:56:50 · 70 阅读 · 0 评论 -
字符串的排列-回溯
题目输入一个字符串,打印出该字符串中字符的所有排列。示例输入:s = “abc”输出:[“abc”,“acb”,“bac”,“bca”,“cab”,“cba”]代码class Solution { List<String> res = new ArrayList<>(); StringBuilder cur = new StringBuilder(); // 用于判重 HashSet<Integer> set.原创 2022-02-14 09:51:33 · 171 阅读 · 0 评论 -
复原IP地址-回溯
题目给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 ‘.’ 来形成。你不能重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。示例输入:s = “25525511135”输出:[“255.255.11.135”,“255.255.111.35”]代码class Solution { List<String> res = new ArrayList<>();.原创 2022-02-11 16:08:38 · 176 阅读 · 0 评论 -
全排列-回溯
题目给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案示例输入:nums = [1,2,3]输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]代码class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> cur = new.原创 2022-02-11 15:40:30 · 433 阅读 · 0 评论 -
分割回文串-动态规划、回溯
题目给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。示例输入:s = “aab”输出:[[“a”,“a”,“b”],[“aa”,“b”]]算法注释代码class Solution { List<List<String>> res = new ArrayList<>(); List<String> cur = new ArrayList<&g.原创 2022-02-11 14:20:30 · 195 阅读 · 0 评论 -
括号生成-回溯
题目数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。示例输入:n = 3输出:["((()))","(()())","(())()","()(())","()()()"]算法代码详细注释代码class Solution { // 保存最终结果 List<String> res = new ArrayList<>(); // 保存当前结果 StringBuilder cu.原创 2022-02-09 15:25:05 · 292 阅读 · 0 评论