add
This commit is contained in:
parent
42bbdd309b
commit
2a0657fa79
41
5.leetcode/src/com/fanxb/common/Q216.java
Normal file
41
5.leetcode/src/com/fanxb/common/Q216.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
39
5.leetcode/src/com/fanxb/common/Q39.java
Normal file
39
5.leetcode/src/com/fanxb/common/Q39.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user