39. 组合总和
https://leetcode-cn.com/problems/combination-sum/
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
if(candidates == null || candidates.length == 0) return list;
Arrays.sort(candidates);
backtrack(list, new ArrayList<>(), candidates, target, 0);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] candidates, int remain, int start){
if(remain < 0){
return;
}else if(remain == 0){
list.add(new ArrayList<>(tmpList));
}else{
for(int i = start; i < candidates.length; i++){
tmpList.add(candidates[i]);
backtrack(list, tmpList, candidates, remain - candidates[i], i);
tmpList.remove(tmpList.size() - 1);
}
}
}
}
40. 组合总和 II
https://leetcode-cn.com/problems/combination-sum-ii/
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
if(candidates == null || candidates.length == 0){
return list;
}
Arrays.sort(candidates);
backtrack(list, new ArrayList<>(), candidates, target, 0);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] candidates, int remain, int start){
if(remain < 0){
return;
}else if(remain == 0){
list.add(new ArrayList<>(tmpList));
}else{
for(int i = start; i < candidates.length; i++){
if(i > start && candidates[i] == candidates[i - 1]) continue;
tmpList.add(candidates[i]);
backtrack(list, tmpList, candidates, remain - candidates[i], i + 1);
tmpList.remove(tmpList.size() - 1);
}
}
}
}
46. 全排列
https://leetcode-cn.com/problems/permutations/
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null || nums.length == 0) return list;
backtrack(list, new ArrayList<>(), nums);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums){
if(tmpList.size() == nums.length){
list.add(new ArrayList<>(tmpList));
}else{
for(int i = 0; i < nums.length; i++){
if(tmpList.contains(nums[i])) continue;
tmpList.add(nums[i]);
backtrack(list, tmpList, nums);
tmpList.remove(tmpList.size() - 1);
}
}
}
}
47. 全排列 II
https://leetcode-cn.com/problems/permutations-ii/
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null || nums.length == 0) return list;
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, new boolean[nums.length]);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, boolean used[]){
if(tmpList.size() == nums.length){
list.add(new ArrayList<>(tmpList));
}else{
for(int i = 0; i < nums.length; i++){
if(used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
tmpList.add(nums[i]);
used[i] = true;
backtrack(list, tmpList, nums, used);
tmpList.remove(tmpList.size() - 1);
used[i] = false;
}
}
}
}
78. 子集
https://leetcode-cn.com/problems/subsets/
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null) return list;
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, int start){
list.add(new ArrayList<>(tmpList));
for(int i = start; i < nums.length; i++){
tmpList.add(nums[i]);
backtrack(list, tmpList, nums, i + 1);
tmpList.remove(tmpList.size() - 1);
}
}
}
90. 子集 II
https://leetcode-cn.com/problems/subsets-ii/
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null) return list;
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, int start){
list.add(new ArrayList<>(tmpList));
for(int i = start; i < nums.length; i++){
if(i > start && nums[i] == nums[i - 1]) continue;
tmpList.add(nums[i]);
backtrack(list, tmpList, nums, i + 1);
tmpList.remove(tmpList.size() - 1);
}
}
}
class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null || nums.length == 0) return list;
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
public static void backtrack(List<List<Integer>> list, ArrayList<Integer> tmpList, int[] nums, int index){
if(index >= nums.length){
if(tmpList.size() >= 2){
list.add(new ArrayList<>(tmpList));
}
return;
}
if(tmpList.isEmpty() || nums[index] >= tmpList.get(tmpList.size() - 1)){
tmpList.add(nums[index]);
backtrack(list, tmpList, nums, index + 1);
tmpList.remove(tmpList.size() - 1);
}
if(index > 0 && !tmpList.isEmpty() && nums[index] == tmpList.get(tmpList.size() - 1)){
return;
}
backtrack(list, tmpList, nums, index + 1);
}
}