排列组合问题集合
无重复数列的子集
问题描述:给定一个无重复数的集合nums,返回所有可能的子集合(包含集合本身和空集)。
比如,如果nums = [1,2,3], 结果是:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
解决代码采用深度优先遍历(dfs):
class Solution{
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result=new ArrayList<>();
List<Integer> tempList=new ArrayList<>();
if(nums==null || nums.length==0)
return result;
dspSubsets(nums,result,tempList,0);
return result;
}
private void dspSubsets(int[] nums,List<List<Integer>> result, List<Integer> tempList,int start)
{
result.add(new ArrayList<>(tempList));
for(int i=start;i<nums.length;i++){
tempList.add(nums[i]);
dspSubsets(nums,result,tempList,i+1);
tempList.remove(tempList.size()-1);
}
}
}
有重复数列的子集
问题描述:给定一个可能包含重复数的集合nums,返回所有可能的子集合(包含集合本身和空集)。
比如,如果nums = [1,2,2], 结果是:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
解决代码采用深度优先遍历(dfs):
class Solution{
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result=new ArrayList<>();
List<Integer> tempList=new ArrayList<>();
Arrays.sort(nums);
dspSubsetsWithDup(nums,result,tempList,0);
return result;
}
private void dspSubsetsWithDup(int[] nums, List<List<Integer>> result, List<Integer> tempList, int start)
{
result.add(new ArrayList<>(tempList));
for(int i=start;i<nums.length;i++)
{
if(i==start || i>start && nums[i]!=nums[i-1]){
tempList.add(nums[i]);
dspSubsetsWithDup(nums,result,tempList,i+1);
tempList.remove(tempList.size()-1);
}
}
}
}
无重复数列的排列
问题描述:给定一个无重复数的集合nums,返回所有可能的排列。
比如,如果nums = [1,2,3], 结果是:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
解决代码采用深度优先遍历(dfs):
class Solution{
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result=new ArrayList<>();
List<Integer> tempList=new ArrayList<>();
if(nums==null || nums.length==0)
return result;
dspPermute(nums,result,tempList);
return result;
}
private void dspPermute(int[] nums, List<List<Integer>> result, List<Integer> tempList)
{
if(tempList.size()==nums.length)
result.add(new ArrayList<>(tempList));
else{
for(int i=0;i<nums.length;i++){
if(tempList.contains(nums[i])) continue;
tempList.add(nums[i]);
dspPermute(nums,result,tempList);
tempList.remove(tempList.size()-1);
}
}
}
}
有重复数列的排列
问题描述:给定一个可能包含重复数的集合nums,返回所有可能的排列。
比如,如果nums = [1,1,2], 结果是:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
解决代码采用深度优先遍历(dfs):
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> result=new ArrayList<>();
List<Integer> tempList=new ArrayList<>();
boolean visited[]=new boolean[nums.length];
Arrays.sort(nums);
permuteUnique(nums,result,tempList,visited);
return result;
}
private void permuteUnique(int[] nums,List<List<Integer>> result,List<Integer> tempList,boolean[] visited)
{
if(tempList.size()==nums.length)
result.add(new ArrayList<>(tempList));
else{
for(int i=0;i<nums.length;i++){
if(visited[i] || (i>0 && nums[i]==nums[i-1] && !visited[i-1])) continue;
visited[i]=true;
tempList.add(nums[i]);
permuteUnique(nums,result,tempList,visited);
tempList.remove(tempList.size()-1);
visited[i]=false;
}
}
}
}
组合数的和
问题描述:给定一个候选数的集合C(不包含重复数)和一个目标数字T,找出在集合C中数字的唯一组合使这些数字的和等于T。
注意:
- 所有的数字都是正整数(包含目标数字)
- 结果不能包含重复的组合
比如,给定候选数集合[2, 3, 6, 7]和目标数字7,
结果集是:
[
[7],
[2, 2, 3]
]
解决代码采用深度优先遍历(dfs):
class Solution{
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result=new ArrayList<>();
List<Integer> tempList=new ArrayList<>();
if(candidates==null || candidates.length==0)
return result;
Arrays.sort(candidates);
dfsCombination(candidates,result,tempList,target,0);
return result;
}
private void dfsCombination(int[] candidates,List<List<Integer>> result,List<Integer> tempList,int target,int index){
if(target<=0) {
if (target == 0)
result.add(new ArrayList<>(tempList));
}else{
for(int i=index;i<candidates.length;i++) {
tempList.add(candidates[i]);
dfsCombination(candidates,result,tempList,target-candidates[i],i);
tempList.remove(tempList.size()-1);
}
}
}
}
组合数的和Ⅱ
问题描述:给定一个候选数的集合C(不包含重复数)和一个目标数字T,找出在集合C中数字的唯一组合使这些数字的和等于T。
集合中的数字在在组合时只能使用一次。
比如,给定候选数集合[10, 1, 2, 7, 6, 1, 5]和目标数字8,
结果集是:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
解决代码采用深度优先遍历(dfs):
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result=new ArrayList<>();
List<Integer> tempList=new ArrayList<>();
if(candidates==null || candidates.length==0)
return result;
Arrays.sort(candidates);
dfsCombinationSum2(candidates,result,tempList,target,0);
return result;
}
private void dfsCombinationSum2(int[] candidates,List<List<Integer>> result,List<Integer> tempList,int target,int index)
{
if(target<0) return;
else if(target==0)
result.add(new ArrayList<>(tempList));
else{
for(int i=index;i<candidates.length;i++){
if(i>index && candidates[i]==candidates[i-1]) continue;
tempList.add(candidates[i]);
dfsCombinationSum2(candidates,result,tempList,target-candidates[i],i+1);
tempList.remove(tempList.size()-1);
}
}
}
}
回文分割
给定一个字符串s,分割这个字符转,让这次分割得到的每一个子字符串都是回文字符串(对称)。
返回所有字符串s的回文分割。
比如,给定s=”aab”,
返回:
[
["aa","b"],
["a","a","b"]
]
解决代码采用深度优先遍历(dfs):
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> result=new ArrayList<>();
List<String> tempList=new ArrayList<>();
if(s==null || s.length()==0)
return result;
dfsPartition(s,result,tempList,0);
return result;
}
private void dfsPartition(String s, List<List<String>> result, List<String> tempList, int index)
{
if(index==s.length())
result.add(new ArrayList<>(tempList));
else{
for(int i=index;i<s.length();i++){
if(isPalindrome(s,index,i)){
tempList.add(s.substring(index,i+1));
dfsPartition(s,result,tempList,i+1);
tempList.remove(tempList.size()-1);
}
}
}
}
private boolean isPalindrome(String s,int low,int high)
{
while (low<high){
if(s.charAt(low++)!=s.charAt(high--)) return false;
}
return true;
}
}