排列組合問題集合

排列組合問題集合


無重複數列的子集

問題描述:給定一個無重複數的集合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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章