【LeetCode】 回溯系列
来源:互联网 发布:证件制作软件手机版 编辑:程序博客网 时间:2024/06/08 18:11
17. Letter Combinations of a Phone Number
题目:返回九宫格输入法所有可能的组合(0-9)
思路:回溯——利用递归的方法一层层实现。
public class Solution { public List<String> letterCombinations(String digits) { List<String> ret = new LinkedList<>(); if(digits == null || digits.length() == 0) return ret; String[] index = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; StringBuilder sb = new StringBuilder(); backtrack(digits, index, sb, 0, ret); return ret; } public void backtrack(String digits, String[] index, StringBuilder sb, int x, List<String> ret){ if(x >= digits.length()){ ret.add(sb.toString()); return; } else{ int len = Integer.valueOf(digits.substring(x, x+1)); for(int i = 0; i < index[len].length(); i++){ sb.append(index[len].charAt(i)); backtrack(digits, index, sb, x+1, ret); sb = sb.deleteCharAt(sb.length()-1); } return; } }}
39. Combination Sum
题目:找到所有和等于目标值的子集。
思路:回溯
public class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> ret = new LinkedList<>(); List<Integer> list = new LinkedList<>(); backtrack(ret, list, candidates, target, 0, 0); return ret; } public void backtrack(List<List<Integer>> ret, List<Integer> list, int[] nums, int target, int sum, int n){ if(sum == target){ ret.add(new LinkedList<Integer>(list)); return; } else if(sum > target){ return; } else{ for(int i = n; i < nums.length; i++){ list.add(nums[i]); backtrack(ret, list, nums, target, sum+nums[i], i); list.remove(list.size()-1); } } }}
40. Combination Sum II
题目:数组有重复
思路:在39题基础上排序+去重即可。
public class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> ret = new LinkedList<>(); List<Integer> list = new LinkedList<>(); Arrays.sort(candidates); backtrack(ret, list, candidates, target, 0, 0); return ret; } public void backtrack(List<List<Integer>> ret, List<Integer> list, int[] nums, int target, int sum, int n){ if(sum == target){ ret.add(new LinkedList<Integer>(list)); return; } else if(sum > target){ return; } else{ for(int i = n; i < nums.length; i++){ if(i != 0 && i != n && nums[i] == nums[i-1]){ continue; } list.add(nums[i]); backtrack(ret, list, nums, target, sum+nums[i], i+1); list.remove(list.size()-1); } } }}
216. Combination Sum III
题目:找到k位之和等于n
思路:感觉跟上面的没啥区别
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> ret = new LinkedList<>(); List<Integer> list = new LinkedList<>(); backtrack(ret, list, k, n, 0, 1); return ret; } public void backtrack(List<List<Integer>> ret, List<Integer> list, int k, int n, int sum, int p){ if(sum == n && list.size() == k){ ret.add(new LinkedList<Integer>(list)); return; } else if(sum > n || list.size() > k){ return; } else{ for(int i = p; i <= 9; i++){ list.add(i); backtrack(ret, list, k, n, sum+i, i+1); list.remove(list.size()-1); } } }}
77. Combinations
题目:找到所有满足固定个数要求的排列。
思路:回溯
public class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> ret = new LinkedList<>(); List<Integer> list = new LinkedList<>(); backtrack(ret, list, n, k, 1); return ret; } public void backtrack(List<List<Integer>> ret, List<Integer> list, int n, int k, int p){ if(list.size() == k){ ret.add(new LinkedList<Integer>(list)); return; } else if(list.size() > k){ return; } else{ for(int i = p; i <= n; i++){ list.add(i); backtrack(ret, list, n, k, i+1); list.remove(list.size()-1); } } }}
78. Subsets
题目:输出所有子集,输入数组不重复
思路:回溯——for循环中设置最远位置
public class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> ret = new LinkedList<>(); List<Integer> list = new LinkedList<>(); for(int i = 0; i <= nums.length; i++) backtrack(ret, list, nums, 0, i); return ret; } public void backtrack(List<List<Integer>> ret, List<Integer> list, int[] nums, int cur, int n){ if(cur == n){ ret.add(new LinkedList<>(list)); return; } else if(cur > n){ return; } else{ for(int i = cur; i < n; i++){ list.add(nums[i]); backtrack(ret, list, nums, i+1, n); list.remove(list.size()-1); } } }}
0 0
- 【LeetCode】 回溯系列
- [leetcode] 回溯法 Combination Sum 系列问题
- leetcode回溯
- 算法系列---回溯算法
- 算法系列---回溯算法
- 算法系列---回溯算法
- 算法系列---回溯算法
- 算法系列---回溯算法
- 回溯、递归系列
- LeetCode 回溯算法 backtracking
- leetcode 回溯法 模板
- [leetcode][回溯] Generate Parentheses
- [leetcode][回溯] Combination Sum
- [leetcode][回溯] Subsets
- [leetcode][回溯] Gray Code
- [leetcode][回溯] Subsets II
- [leetcode][回溯] Combinations
- LeetCode Sudoku Solver 回溯
- JSON和JSONP的区别
- 796A
- 捕鱼来了2017系列游戏
- MyBatis传参数四种方法
- 4.26课堂作业 --转换构造函数
- 【LeetCode】 回溯系列
- 八皇后算法
- Problem F: 分数类的模板数组类
- 线段树
- Windows通过 iis创建FTP服务
- 【ACM】P1000、P10001、P1002、P1003代码演示
- BW 库存管理之processkey
- Comparable和Comparator
- 从此以后,江湖有了它的传说!