【LeetCode】77. Combination +39. Combination sum +40. Combination sum II

Topic link: 77. Combination
Description of the topic: Given two integers n and k, return all possible combinations of k numbers in 1...n.
Example:

输入: n = 4, k = 2
输出:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

Idea: conventional solution, recursion + backtracking, use a subscript startIndex to indicate the starting point of the current search, while searching will record the current value on this path, after searching for a round of answers, backtracking, delete the last value, and change the path Keep searching.
Code:

class Solution {
    
    
    public void dfs(int n,int startIndex,List<Integer> list,List<List<Integer>> ans,int k){
    
    
        if(k==list.size()){
    
    
            List<Integer> list1=new ArrayList<>(list);
            ans.add(list1);
            return ;
        }
        for(int i=startIndex;i<=n;i++){
    
    
            list.add(i);
            dfs(n,i+1,list,ans,k);
            list.remove(list.size()-1);
        }
    }
    public List<List<Integer>> combine(int n, int k) {
    
    
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        if(n<k||k<=0){
    
    
            return ans;
        }
        List<Integer> list=new ArrayList<>();
        dfs(n,1,list,ans,k);
        return ans;
    }
}

Similar topics: 46. ​​Full permutation +47. Full permutation II. For
further details , see 39. Combination sum
problem description: Given an array of candidates without repeated elements and a target number target, find out all candidates that can make the sum of numbers target. combination.

The numbers in candidates can be repeatedly selected without limitation.
Example:

输入:candidates = [2,3,6,7], target = 7,
所求解集为:
[
  [7],
  [2,2,3]
]
输入:candidates = [2,3,5], target = 8,
所求解集为:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

data range:

1 <= candidates.length <= 30
1 <= candidates[i] <= 200
candidate 中的每个元素都是独一无二的。
1 <= target <= 500

Idea: Similar to the above idea, except that each element here can be reused, so when searching, start from the current node.
Code:

class Solution {
    
    
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
    
    
        List<Integer> list=new ArrayList<>();
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        dfs(candidates,0,list,ans,target);
        return ans;
    }
    public void dfs(int nums[],int startIndex,List<Integer> list,List<List<Integer>> ans,int target){
    
    
        if(target<0) return;
        if(target==0){
    
    
            ans.add(new ArrayList<>(list));
            return ;
        }
        for(int i=startIndex;i<nums.length;i++){
    
    
            list.add(nums[i]);
            dfs(nums,i,list,ans,target-nums[i]);//可以重复利用每个元素,因此还是从当前位置开始搜
            list.remove(list.size()-1);
        }
    }

}

Title link: 40. Combination Sum II
Title Description: Given an array of candidates and a target number target, find out all combinations of candidates that can make the sum of numbers target.

Each number in candidates can only be used once in each combination.

  • All numbers (including the target number) are positive integers.
  • The solution set cannot contain repeated combinations.

Example:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
  [1,2,2],
  [5]
]

Idea: A number can only be used once, the method is to sort the order first, and then continue directly if two adjacent ones are the same to avoid repeating the record of the answer.

class Solution {
    
    
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
    
    
        Arrays.sort(candidates);
        List<Integer> list=new ArrayList<>();
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        dfs(candidates,target,list,ans,0);
        return ans;
    }
    public void dfs(int []nums,int target,List<Integer> list,List<List<Integer>> ans,int startIndex){
    
    
        if(target<0) return;
        if(target==0){
    
    
            List<Integer> list1=new ArrayList<>(list);
            ans.add(list1);
            return;
        }
        for(int i=startIndex;i<nums.length;i++){
    
    
            if(i>startIndex&&nums[i] == nums[i-1]) continue;//避免出现重复情况
            list.add(nums[i]);
            dfs(nums,target-nums[i],list,ans,i+1);
            list.remove(list.size()-1);
        }
    }
}

Guess you like

Origin blog.csdn.net/dl962454/article/details/115023866