LeetCode back Theme 4: combinatorial problems Combinations

Experience again the importance of a recursive structure analysis, draw the tree is the key. And the initial perception of a recursive branch can be built.

Example: LeetCode 77 title

Portal: English Website: 77. Combinations , Chinese website: 77. combination .

Given two integers n and k , ... return. 1 n possible all k combination number.

Example:

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

Analysis: This question is drawing step is critical analysis, we must draw the recursion tree, we can get a clear idea of ​​solving problems. The following figure shows the use of a code execution flow back method.

Solving the key: in order to find, has used digital will no longer use, so do not set the marked array. It focuses on the traverse upper bound i was n - (k - stack.size()) + 1.

414598-d2872c89dbdd3562.jpg
77 LeetCode question: composition -1

The following diagram shows how to analyze the loop variable in ithe community. (If the following image is too small, you can right, select "Open in new tab images" on the image to view a larger image.)

414598-0f99d173bac7fff8.jpg
LeetCode question 77: combination -2

In order to take, i.e., a route search according to certain rules to, and can guarantee not be repeated. Combinatorial problem, regardless of the order, but when we do, according to the order do.

Java code implementation: Find the order, we have used digital will no longer use, so do not set used array.

public class Solution {

    private List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> combine(int n, int k) {
        if (n <= 0 && k <= 0 && k > n) {
            return result;
        }

        // 从 1 开始
        generateCombinations(n, k, 1, new ArrayList<>());
        return result;
    }

    /**
     * 从 [1,n] 中选出 k 个数,
     *
     * @param n     从 [1,n] 中选
     * @param k     选出的数字的个数
     * @param start 当前被选中的起始数字
     * @param pre   已经构成的数字列表
     */
    private void generateCombinations(int n, int k, int start, List<Integer> pre) {
        if (pre.size() == k) { // pre.size() == k
            result.add(new ArrayList<>(pre));
            return;
        }
        for (int i = start; i <= n; i++) {
            pre.add(i);
            generateCombinations(n, k, i + 1, pre);
            pre.remove(pre.size() - 1);
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<List<Integer>> combine = solution.combine(4, 2);
        System.out.println(combine);
    }
}

Here there is a version of the code was written when I exercise.

to sum up:

1, whether it is a combination of permutation problem or question, we have to follow a certain order to find answers to your questions here, "order" and "arrangement" are two concepts;

2, although a combination of problems, but the order in which the search process has already ruled out the possibility of duplication;

3, must be compared with an arrangement of the problem to solve combinatorial problems Experience: Troubleshooting combination is repeated in the process embodied in the branch, are arranged need to solve the problem by means of a repeating array.

In fact, although here we have been able to submit to LeetCode Accepted, but this algorithm as well as the optimization of space.

(End of this section)

Guess you like

Origin blog.csdn.net/weixin_34092370/article/details/90885290