39 组合总和
https://leetcode.cn/problems/combination-sum/description/
两种取法,第一种,每层决定一个数取几个,一共n层,每层分支数最多为target/min candidate; 第二种,每种取一个数,直到取到target或超过,一共target/min candidate层,每层分支最多为n。
class Solution { //方法一
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
dfs(result, cur, candidates, target, 0);
return result;
}
private void dfs(List<List<Integer>> result, List<Integer> cur, int[] candidates, int remaining, int index) {
if (remaining == 0) {
result.add(new ArrayList<>(cur));
}
for (int i = index; i < candidates.length; i++) {
int len = cur.size();
for (int j = 1; j <= remaining / candidates[i]; j++) {
for (int k = 1; k <= j; k++) {
cur.add(candidates[i]);
}
dfs(result, cur, candidates, remaining - j * candidates[i], i + 1);
cur.subList(len, cur.size()).clear();
}
}
}
}
class Solution { //方法二
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
dfs(result, cur, candidates, target, 0);
return result;
}
private void dfs(List<List<Integer>> result, List<Integer> cur, int[] candidates, int remaining, int index) {
if (remaining <= 0) { //如果不可能再放数就返回
if (remaining == 0) result.add(new ArrayList<>(cur)); //如果正好相等了就记录
return;
}
for (int i = index; i < candidates.length; i++) {
cur.add(candidates[i]);
dfs(result, cur, candidates, remaining - candidates[i], i); //下一层还能再拿index为i的这个数,向下dfs时,index = i 而不是 i+ 1
cur.remove(cur.size() - 1);
}
}
}
40 组合总和II
https://leetcode.cn/problems/combination-sum-ii/description/
去重是横向层去重,一旦不要某个数,那么同一层相同的就都不要,体现在向下dfs时,取candidate[i]时,cadidate[i]要么是这层第一个能取的数,要么它和前面都不要的数不一样。
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
Arrays.sort(candidates);
dfs(result, cur, candidates, target, 0);
return result;
}
private void dfs(List<List<Integer>> result, List<Integer> cur, int[] candidates, int remaining, int index) {
if (remaining <= 0) { //如果不可能再放数就返回
if (remaining == 0) result.add(new ArrayList<>(cur)); //如果正好相等了就记录
return;
}
for (int i = index; i < candidates.length; i++) {
if (i == index || candidates[i] != candidates[i - 1]) {
cur.add(candidates[i]);
dfs(result, cur, candidates, remaining - candidates[i], i + 1); //下一层不能再拿index为i的这个数,向下dfs时,index = i+ 1
cur.remove(cur.size() - 1);
}
}
}
}
131 分割回文串
https://leetcode.cn/problems/palindrome-partitioning/
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<>();
List<String> cur = new ArrayList<>();
dfs(result, cur, 0, s);
return result;
}
private void dfs(List<List<String>> result, List<String> cur, int index, String s) {
if (index == s.length()) {
result.add(new ArrayList<>(cur));
return;
}
for (int i = index; i < s.length(); i++) {
if (valid(index, i, s)) {
cur.add(s.substring(index, i + 1));
dfs(result, cur, i + 1, s);
cur.remove(cur.size() - 1);
}
}
}
private boolean valid(int start, int end, String s) {
while (start < end) {
if (s.charAt(start) != s.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}