# 子集，组合，全排列

2020/12/28 20:04

#### 思路分析

result = []
def backtrack(路径, 选择列表):
if 满足结束条件:
return
for 选择 in 选择列表:
做选择
backtrack(路径, 选择列表)
撤销选择


#### 子集题目解题代码

public class Subsets {

/**
* result = []
* def backtrack(路径, 选择列表):
*     if 满足结束条件:
*         return
*     for 选择 in 选择列表:
*         做选择
*         backtrack(路径, 选择列表)
*         撤销选择
*/

if (nums == null) {
return null;
}

findResluts(nums, 0, ls);

return solutions;

}

private void findResluts(int[] nums, int start, LinkedList<Integer> integers) {

for (int i = start; i < nums.length; i++) {
findResluts(nums, i + 1, integers);
integers.pollLast();
}
}

public static void main(String[] args) {
new Subsets().subsets(new int[]{1, 2, 3});
}
}
}


#### 集合解题代码

class Solution {

List<List<Integer>> combineSolutions = new ArrayList<List<Integer>>();

public List<List<Integer>> combine(int n, int k) {

ArrayList<Integer> ls = new ArrayList<>();

findCombineResult(1,n,k, ls);

return combineSolutions;
}

private void findCombineResult(int start,int n,int k, ArrayList<Integer> integers){
if (integers.size() == k){
}
for (int i = start; i <= n; i++) {
findCombineResult(i + 1, n,k,integers);
integers.remove(integers.size() - 1);
}

}


#### 全排列解题代码

class Solution {

private List<List<Integer>> res = new LinkedList<>();

public List<List<Integer>> permute(int[] nums) {

goCal(nums,tracks);

return res;
}

if (nums.length == tracks.size()) {

return;
}

for (int num :nums){

if (tracks.contains(num)){
continue;
}

goCal(nums,tracks);

tracks.removeLast();

}

}

}


0 评论
0 收藏
0