算法学习day27回溯part03-39、40、131
package LeetCode.backtrackpart03; import java.lang.management.LockInfo; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; /** * 39. 组合总和 * 给你一个 无重复元素 的整数数组 candidates 和一个目标整数target, * 找出candidates中可以使数字和为目标数target 的 所有不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。 * candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。 * 对于给定的输入,保证和为target 的不同组合数少于 150 个。 * 示例: * 输入:candidates = [2,3,6,7], target = 7 * 输出:[[2,2,3],[7]] * */ public class CombinationSum_39 { public static void main(String[] args) { int [] candidates = {2,3,6,7}; int target = 7; List<List<Integer>> result = new ArrayList<>(); result = combinationSum(candidates,target); System.out.println(result); } public static List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(candidates); // 先进行排序 backtracking(res, new ArrayList<>(), candidates, target, 0, 0); return res; } public static void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int idx) { // 找到了数字和为 target 的组合 if (sum == target) { res.add(new ArrayList<>(path)); return; } for (int i = idx; i < candidates.length; i++) { // 如果 sum + candidates[i] > target 就终止遍历 if (sum + candidates[i] > target) break; path.add(candidates[i]); backtracking(res, path, candidates, target, sum + candidates[i], i); path.remove(path.size() - 1); // 回溯,移除路径 path 最后一个元素 } } }
package LeetCode.backtrackpart03; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; /** * 40. 组合总和 II * 给定一个候选人编号的集合 candidates 和一个目标数 target,找出 candidates 中所有可以使数字和为 target 的组合。 * candidates中的每个数字在每个组合中只能使用一次。 * 注意:解集不能包含重复的组合。 * 示例: * 输入: candidates = [10,1,2,7,6,1,5], target = 8, * 输出: * [[1,1,6],[1,2,5],[1,7],[2,6]] * */ public class CombinationSumII_40 { static LinkedList<Integer> path = new LinkedList<>(); static List<List<Integer>> ans = new ArrayList<>(); static boolean[] used; static int sum = 0; public static void main(String[] args) { int [] array = {10,1,2,7,6,1,5}; int target = 8; ans = combinationSum2(array,target); System.out.println(ans); } public static List<List<Integer>> combinationSum2(int[] candidates, int target) { used = new boolean[candidates.length]; // 加标志数组,用来辅助判断同层节点是否已经遍历 Arrays.fill(used, false); // 为了将重复的数字都放到一起,所以先进行排序 Arrays.sort(candidates); backTracking(candidates, target, 0); return ans; } private static void backTracking(int[] candidates, int target, int startIndex) { if (sum == target) { ans.add(new ArrayList(path)); } for (int i = startIndex; i < candidates.length; i++) { if (sum + candidates[i] > target) { break; } // 出现重复节点,同层的第一个节点已经被访问过,所以直接跳过 if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) { continue; } used[i] = true; sum += candidates[i]; path.add(candidates[i]); // 每个节点仅能选择一次,所以从下一位开始 backTracking(candidates, target, i + 1); used[i] = false; sum -= candidates[i]; path.removeLast(); } } }
package LeetCode.backtrackpart03; import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; import java.util.List; /** * 131. 分割回文串 * 给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。 * 回文串 是正着读和反着读都一样的字符串。 * 示例: * 输入:s = "aab" * 输出:[["a","a","b"],["aa","b"]] * */ public class PalindromePartitioning_131 { static List<List<String>> lists = new ArrayList<>(); static Deque<String> deque = new LinkedList<>(); public static void main(String[] args) { String str = "abc"; lists = partition(str); System.out.println(lists); } public static List<List<String>> partition(String s) { backTracking(s, 0); return lists; } public static void backTracking(String s, int startIndex) { //如果起始位置大于s的大小,说明找到了一组分割方案 if (startIndex >= s.length()) { lists.add(new ArrayList(deque)); return; } for (int i = startIndex; i < s.length(); i++) { //如果是回文子串,则记录 if (isPalindrome(s, startIndex, i)) { String str = s.substring(startIndex, i + 1); deque.addLast(str); } else { continue; } //起始位置后移,保证不重复 backTracking(s, i + 1); deque.removeLast(); } } //判断是否是回文串 public static boolean isPalindrome(String s, int startIndex, int end) { for (int i = startIndex, j = end; i < j; i++, j--) { if (s.charAt(i) != s.charAt(j)) { return false; } } return true; } }
分类:
算法学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署