回溯+dfs總結(jié)

1.leetcode46.全排列
給定一個(gè)沒有重復(fù)數(shù)字的序列,返回其所有可能的全排列。
輸入: [1,2,3]
輸出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<Integer>();
    boolean[] visited;

    public List<List<Integer>> permute(int[] nums) {
        visited = new boolean[nums.length];
        dfs(nums,0);
        return ans;
    }

    public void dfs(int[]nums,int u){
        if(u==nums.length){
            List<Integer> temp = new ArrayList<Integer>(path);
            ans.add(temp);
        }

        for(int i = 0;i<nums.length;i++){
            if(!visited[i]){
                visited[i] = true;
                path.add(nums[i]);
                dfs(nums,u+1);
                visited[i] = false;
                path.remove(path.size()-1);
            }
        }
    }
}

2. leetcode 47 全排列II
給定一個(gè)可包含重復(fù)數(shù)字的序列,返回所有不重復(fù)的全排列。
輸入: [1,1,2]
輸出:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

題解1:
class Solution {

    List<List<Integer>> ans = new ArrayList<>();
    int[] path;
    public List<List<Integer>> permuteUnique(int[] nums) {
        if(nums.length==0) return ans;
        path = new int[nums.length];
        Arrays.sort(nums);
        dfs(nums,0,0,0);
        return ans;
    }

    //u是當(dāng)前數(shù),start代表下一個(gè)枚舉位置 state的二進(jìn)制數(shù)表示第i位是否被占用
    public void dfs(int[]nums,int u,int start,int state){
        if(u==nums.length){
            List<Integer>temp = new ArrayList<>();
            for(int x:path){
                temp.add(x);
            }
            ans.add(temp);
            return;
        }

        if(u==0||nums[u]!=nums[u-1]) start = 0;
        for(int i =start;i<nums.length;i++){
            //判斷當(dāng)前位置有沒有被使用過
            if((state>>i&1)==0){
                path[i] = nums[u];
                dfs(nums,u+1,i+1,state+(1<<i));

            }
        }



    }
}

3. leetcode78 子集
給定一組不含重復(fù)元素的整數(shù)數(shù)組 nums,返回該數(shù)組所有可能的子集(冪集)。

說明:解集不能包含重復(fù)的子集。

示例:
輸入: nums = [1,2,3]
輸出:
[
 [3],
 [1],
 [2],
 [1,2,3],
 [1,3],
 [2,3],
 [1,2],
 []
]
class Solution {
    //用回溯法
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        for(int i=0;i<=nums.length;i++){
            dfs(nums,i,0);
        }
        return ans;
    }

    public void dfs(int[]nums,int n,int s){
        if(path.size()==n){
            List<Integer> temp = new ArrayList<>(path);
            ans.add(temp);
            return;
        }

        for(int i = s; i<nums.length;i++){
            path.add(nums[i]);
            dfs(nums,n,i+1);
            path.remove(path.size()-1);
        }

    }

}

leetcode 90 子集II

給定一個(gè)可能包含重復(fù)元素的整數(shù)數(shù)組 nums,返回該數(shù)組所有可能的子集(冪集)。

說明:解集不能包含重復(fù)的子集。

示例:

輸入: [1,2,2]
輸出:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if(nums.length==0) return ans;
        Arrays.sort(nums);
        List<Integer> t = new ArrayList<>();
        //先加入一個(gè)空集合
        ans.add(t);
        for(int i=1;i<=nums.length;i++){
            dfs(nums,i,0);
        }
        return ans;
    }

    public void dfs(int[]nums,int n,int s){
        if(path.size()==n){
            List<Integer> temp = new ArrayList<>(path);
            ans.add(temp);
            return;
        }


        for(int i = s;i<nums.length;i++){
            if(i!=s&&nums[i] == nums[i-1]) continue;
             path.add(nums[i]);
             dfs(nums,n,i+1);
             path.remove(path.size()-1);

        }


    }

}


leetcode 39 組數(shù)總和 I
給定一個(gè)無重復(fù)元素的數(shù)組 candidates 和一個(gè)目標(biāo)數(shù) target ,找出 candidates 中所有可以使數(shù)字和為 target 的組合。

candidates 中的數(shù)字可以無限制重復(fù)被選取。

說明:

所有數(shù)字(包括 target)都是正整數(shù)。
解集不能包含重復(fù)的組合。 
示例 1:

輸入: candidates = [2,3,6,7], target = 7,
所求解集為:
[
  [7],
  [2,2,3]
]
示例 2:

輸入: candidates = [2,3,5], target = 8,
所求解集為:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        if(candidates.length==0) return ans;
        Arrays.sort(candidates);
        dfs(candidates,target,0);
        return ans;
    }

    public void dfs(int[]nums,int target,int s){
        if(target==0){
            List<Integer> temp = new ArrayList<>(path);
            ans.add(temp);
            return;
        }

        for(int i = s;i<nums.length;i++){
            if(nums[i]>target) break;
            path.add(nums[i]);
            dfs(nums,target-nums[i],i);
            path.remove(path.size()-1);
        }
    }


}
leetcode 40 組數(shù)總和II
給定一個(gè)數(shù)組 candidates 和一個(gè)目標(biāo)數(shù) target ,找出 candidates 中所有可以使數(shù)字和為 target 的組合。

candidates 中的每個(gè)數(shù)字在每個(gè)組合中只能使用一次。

說明:

所有數(shù)字(包括目標(biāo)數(shù))都是正整數(shù)。
解集不能包含重復(fù)的組合。 
示例 1:

輸入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集為:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]
示例 2:

輸入: candidates = [2,5,2,1,2], target = 5,
所求解集為:
[
  [1,2,2],
  [5]
]

class Solution {
    
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        if(candidates.length==0) return ans;
        Arrays.sort(candidates);
        dfs(candidates,target,0);
        return ans;
    }
    
    public void dfs(int[]nums,int target,int s){
          
        if(target==0){
            List<Integer> temp = new ArrayList<>(path);
            ans.add(temp);
            return;
        }
        
        for(int i = s;i<nums.length;i++){
            if (i > s && nums[i] == nums[i - 1]) continue;
            if(nums[i] > target) break;
            path.add(nums[i]);
            dfs(nums,target-nums[i],i+1);
            path.remove(path.size()-1);
        }
    }
    
}

leetcode 216 組數(shù)總和III
找出所有相加之和為 n 的 k 個(gè)數(shù)的組合。組合中只允許含有 1 - 9 的正整數(shù),并且每種組合中不存在重復(fù)的數(shù)字。

說明:

所有數(shù)字都是正整數(shù)。
解集不能包含重復(fù)的組合。 
示例 1:

輸入: k = 3, n = 7
輸出: [[1,2,4]]
示例 2:

輸入: k = 3, n = 9
輸出: [[1,2,6], [1,3,5], [2,3,4]]


class Solution {
    
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    
    public List<List<Integer>> combinationSum3(int k, int n) {
        int[]nums = new int[10];
        for(int i = 1;i<=9;i++){
            nums[i] = i;
        }
        
        dfs(nums,n,k,1);
        return ans;
    }
    
    public void dfs(int[]nums,int target,int len,int s){
        if(path.size()>len) return;
        
        if(target==0 && path.size()==len){
            List<Integer> temp = new ArrayList<>(path);
            ans.add(temp);
            return;
        }
        
        for(int i = s;i<nums.length;i++){
            if(nums[i]>target) break;
            path.add(nums[i]);
            dfs(nums,target-nums[i],len,i+1);
            path.remove(path.size()-1);
        }
        
        
    }
    
    
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容