【LeetCode】 回溯系列
2017-04-26 15:24
309 查看
17. Letter Combinations of a Phone Number
题目:返回九宫格输入法所有可能的组合(0-9)
思路:回溯——利用递归的方法一层层实现。
39. Combination Sum
题目:找到所有和等于目标值的子集。
思路:回溯
40. Combination Sum II
题目:数组有重复
思路:在39题基础上排序+去重即可。
216. Combination Sum III
题目:找到k位之和等于n
思路:感觉跟上面的没啥区别
77. Combinations
题目:找到所有满足固定个数要求的排列。
思路:回溯
78. Subsets
题目:输出所有子集,输入数组不重复
思路:回溯——for循环中设置最远位置
题目:返回九宫格输入法所有可能的组合(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); } } } }
相关文章推荐
- Combination Sum 系列问题(leetcode dfs回溯,动归)由浅入深DFS
- 从Leetcode的Combination Sum系列谈起回溯法
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
- 算法洗脑系列(8篇)——第六篇 回溯思想
- LeetCode 笔记系列十 Suduko
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
- LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]
- LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]
- LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- 算法洗脑系列(8篇)——第六篇 回溯思想
- LeetCode 笔记系列三 3Sum
- LeetCode 笔记系列七 Substring with Concatenation of All Words
- LeetCode 笔记系列九 Search in Rotated Sorted Array
- LeetCode 笔记系列四 Remove Nth Node From End of List
- leetcode: 2Sum/3Sum/3SumClosest/4Sum系列问题
- hdu1506 & leetcode84 Largest Rectangle in a Histogram 矩阵系列(一)
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
- LeetCode 笔记系列 19 Scramble String [合理使用递归]