77. 组合
题目
给定两个整数 n
和 k
,返回范围 [1, n]
中所有可能的 k
个数的组合。
答案
class Solution {
List<List<Integer>> res = new ArrayList();
List<Integer> list = new LinkedList();
public List<List<Integer>> combine(int n, int k) {
deal(n,1,k);
return res;
}
void deal(int n,int startIndex,int k){
if(list.size()==k){
res.add(new ArrayList(list));
return;
}
for(int i=startIndex;i<=n;i++){
list.add(i);
deal(n,i+1,k);
list.removeLast();
}
}
}
216. 组合总和 III
题目
找出所有相加之和为 n
的 k
个数的组合,且满足下列条件:
- 只使用数字1到9
- 每个数字 最多使用一次
返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。
答案
class Solution {
List<List<Integer>> res = new ArrayList();
List<Integer> list = new LinkedList();
public List<List<Integer>> combinationSum3(int k, int n) {
deal(1,k,n,0);
return res;
}
void deal(int startIndex,int k,int n,int sum){
if(list.size()>k || sum>n){//剪枝操作
return;
}
if(list.size()==k && sum==n){
res.add(new ArrayList(list));
return;
}
for(int i=startIndex;i<=9;i++){
list.add(i);
deal(i+1,k,n,sum+i);
list.removeLast();
}
}
}
17. 电话号码的字母组合
题目
给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
答案
class Solution {
List<String> res = new ArrayList();
StringBuilder sb = new StringBuilder();
String[] strs = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public List<String> letterCombinations(String digits) {
if(digits.length()==0){
return res;
}
deal(digits,0);
return res;
}
void deal(String digits,int depth){
if(depth==digits.length()){
res.add(sb.toString());
return;
}
String str = strs[digits.charAt(depth)-'0'];
for(int i=0;i<str.length();i++){
sb.append(str.charAt(i));
deal(digits,depth+1);
sb.deleteCharAt(sb.length()-1);
}
}
}
39. 组合总和
题目
给你一个 无重复元素 的整数数组 candidates
和一个目标整数 target
,找出 candidates
中可以使数字和为目标数 target
的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates
中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target
的不同组合数少于 150
个。
答案
class Solution {
List<List<Integer>> res = new ArrayList();
List<Integer> list = new LinkedList();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
deal(candidates,0,0,target);
return res;
}
void deal(int[] candidates,int startIndex,int sum,int target){
if(sum>target){
return;
}
if(sum==target){
res.add(new ArrayList(list));
return;
}
for(int i=startIndex;i<candidates.length;i++){
list.add(candidates[i]);
deal(candidates,i,sum+candidates[i],target);
list.removeLast();
}
}
}
40. 组合总和 II
题目
给定一个候选人编号的集合 candidates
和一个目标数 target
,找出 candidates
中所有可以使数字和为 target
的组合。
candidates
中的每个数字在每个组合中只能使用 一次 。
答案
class Solution {
List<List<Integer>> res = new ArrayList();
List<Integer> list = new LinkedList();
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
deal(candidates,0,0,target);
return res;
}
void deal(int[] candidates,int startIndex,int sum,int target){
if(sum>target){
return;
}
if(sum==target){
res.add(new ArrayList(list));
return;
}
for(int i=startIndex;i<candidates.length;i++){
if(i>startIndex && candidates[i]==candidates[i-1]){
continue;
}
list.add(candidates[i]);
deal(candidates,i+1,sum+candidates[i],target);
list.removeLast();
}
}
}