Brushing Questions Diary 05 "Backtracking Algorithm"

Problem Description

Leetcode icon-default.png?t=N5K3https://leetcode.cn/problems/TVdhkn/

Given an integer array nums , the elements in the array are different from each other. Returns all possible subsets (power sets) of this array.

A solution set cannot contain duplicate subsets. You can return the solution sets in any order.

Example 1:

Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1, 2,3]]
Example 2:

Input: nums = [0]
Output: [[],[0]]

hint:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums All elements in  are distinct from each other

problem solving ideas

The classic application of the backtracking problem: find a sub-set of a queue, and require that sets of the same number in different orders in the sub-set are also considered to be the same set. The first question we have to face is: how to ensure that our final solution problem does not contain duplicate sets? The method I use is: use the relative position of the numbers in the queue, that is, do not change the relative position in the queue, and jump to select whether the current position is added to the target queue. Next, we will explain the overall idea: set the subscript, start from the first number At the beginning, each number has two options of joining or not joining the target queue. When the subscript points to the back of the last element, the current data set can be added to the result queue (this also ensures the relative position between the numbers: only from going to after the selection)

example code

class Solution {
    private LinkedList<List<Integer>>res=new LinkedList<>();
    private LinkedList<Integer>data=new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        reverse(nums,0);
        return res;
    }
    public void reverse(int[]nums,int index){
        //递归出口
        if(index==nums.length){
            res.add(new ArrayList(data));
            return ;
        }
        //先递归没有下标的
        reverse(nums,index+1);
        data.addLast(nums[index]);
        reverse(nums,index+1);
        //回溯
        data.removeLast();

    }
}

Time complexity O(N*2^N) Space complexity: O(N)

Problem Description

Given two integers n and k, return all possible combinations of k numbers from 1 ... n.

Example 1:

Input: n = 4, k = 2
Output:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]
Example 2:

Input: n = 1, k = 1
Output: [[1]]

 

hint:

1 <= n <= 20
1 <= k <= n

problem solving ideas

The idea is exactly the same as the above question, but it is necessary to judge whether the current number is satisfied at the final termination condition. If it is satisfied, it will join the target queue. If it is not satisfied, there is no need to join

example code

class Solution {
    private List<List<Integer>>res=new LinkedList<>();
    private LinkedList<Integer>data=new LinkedList<>();
    public List<List<Integer>> combine(int n, int k) {
        //创建数组
        int[]arr=new int[n];
        for(int i=1;i<=n;++i){
            arr[i-1]=i;
        }
        //调用函数
        reverse(0,arr,k);
        return res;
    }
    public void reverse(int index,int[]arr,int k){
        //递归出口
        if(index==arr.length){
            if(data.size()==k){
                res.add(new LinkedList(data));
            }
            return;
        }
        //进行递归
        reverse(index+1,arr,k);
        data.addLast(arr[index]);
        reverse(index+1,arr,k);
        //回溯
        data.removeLast();
    }

}

 

Guess you like

Origin blog.csdn.net/m0_65431718/article/details/131534094