This commit is contained in:
fanxb 2022-03-10 17:36:04 +08:00
parent 42bbdd309b
commit 2a0657fa79
3 changed files with 99 additions and 23 deletions
5.leetcode/src/com/fanxb/common

@ -0,0 +1,41 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class Q216 {
public List<List<Integer>> combinationSum3(int k, int n) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
List<List<Integer>> res = new ArrayList<>();
dfs(arr, n, k, 0, new Stack<>(), res);
return res;
}
private void dfs(int[] candidates, int n, int k, int index, Stack<Integer> temp, List<List<Integer>> res) {
if (n == 0) {
//说明找到一个结果序列
if (temp.size() == k) {
//只有刚好k个数才是结果
res.add(new ArrayList<>(temp));
}
return;
}
for (int i = index; i < candidates.length; i++) {
if (candidates[i] > n || temp.size() == k) {
//前面已经排序过所以在这里可以进行剪枝操作如果candidates[index]都小于target了那就不需要比较后面的了肯定不满足要求
//另外只能使用k个数所以当temp的size为k时说明不能在装了本条路结束
return;
}
temp.push(candidates[i]);
dfs(candidates, n - candidates[i], k, i + 1, temp, res);
temp.pop();
}
}
public static void main(String[] args) {
new Q216().combinationSum3(3, 9).forEach(System.out::println);
}
}

@ -0,0 +1,39 @@
package com.fanxb.common;
import java.util.*;
/**
* TODO
*
* @author fanxb
* @date 2022/3/10 15:10
*/
public class Q39 {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
dfs(candidates, target, 0, new Stack<>(), res);
return res;
}
private void dfs(int[] candidates, int target, int index, Stack<Integer> temp, List<List<Integer>> res) {
if (target == 0) {
//说明找到一个结果序列
res.add(new ArrayList<>(temp));
return;
}
for (int i = index; i < candidates.length; i++) {
if (candidates[i] > target) {
//前面已经排序过所以在这里可以进行剪枝操作如果candidates[index]都小于target了那就不需要比较后面的了肯定不满足要求
return;
}
temp.push(candidates[i]);
dfs(candidates, target - candidates[i], i, temp, res);
temp.pop();
}
}
public static void main(String[] args) {
new Q39().combinationSum(new int[]{2, 3, 5}, 8).forEach(System.out::println);
}
}

@ -1,44 +1,40 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
public class Q40 {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
this.deal(res, new LinkedList<>(), 0, candidates, target);
dfs(candidates, target, 0, new Stack<>(), res);
return res;
}
private void deal(List<List<Integer>> res, List<Integer> cur, int start, int[] sources, int target) {
if (start >= sources.length || target < sources[start]) {
private void dfs(int[] candidates, int target, int index, Stack<Integer> temp, List<List<Integer>> res) {
if (target == 0) {
//说明找到一个结果序列
res.add(new ArrayList<>(temp));
return;
}
for (int i = start; i < sources.length; i++) {
if (sources[i] > target) {
for (int i = index; i < candidates.length; ) {
if (candidates[i] > target) {
//前面已经排序过所以在这里可以进行剪枝操作如果candidates[index]都小于target了那就不需要比较后面的了肯定不满足要求
return;
}
if (i > start && sources[i] == sources[i - 1]) {
//第二个重复的元素不需要进行后续操作
continue;
temp.push(candidates[i]);
dfs(candidates, target - candidates[i], i + 1, temp, res);
temp.pop();
//手动控制i的增长对于同一个数字不能重复处理
int nextI = i + 1;
while (nextI < candidates.length && candidates[nextI] == candidates[i]) {
nextI++;
}
cur.add(sources[i]);
if (sources[i] == target) {
res.add(new ArrayList<>(cur));
} else {
deal(res, cur, i + 1, sources, target - sources[i]);
}
//加入后删除当前元素尝试下一个
cur.remove(cur.size() - 1);
i = nextI;
}
}
public static void main(String[] args) {
new Q40().combinationSum2(new int[]{2, 5, 2, 1, 2}, 5);
new Q40().combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8).forEach(System.out::println);
}
}