Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find the combination of backtracking algorithm

Shulou Source: shulou.com Published: 2022-06-03 03:43:37 09月14日 Update

This article introduces the relevant knowledge of "how to find a combination of backtracking algorithms". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Is the backtracking algorithm almost forgotten? do you still remember how to solve the combinatorial problem? Ha ha ha

Backtracking algorithm is actually a violent search, since it is a violent search, why do we have to use backtracking? Because some problems can be violently searched out is good, can not find a better way.

Given two integers n and k, return 1. The combination of all possible k numbers in n.

If you use for loops nested layer by layer to solve this problem, if n is 100 and k is 50, then 50 layers of for loops, then you will find that simple violence is not possible.

The backtracking algorithm is on the scene.

Cascading nesting of for loops with recursion in backtracking algorithm (it can be understood as opening k-layer for loops)

If a for loop is nested in each recursion, then recursion can solve the problem of multi-layer nested loops.

I am in the article backtracking algorithm: find the combination problem! At the same time, it also gives a retrospective trilogy. According to this method, it is found that the backtracking algorithm is not difficult.

Topic link: https://leetcode-cn.com/problems/combinations/

The backtracking algorithm template is as follows:

Void backtracking (parameter) {if (termination condition) {store the result; return;} for (Select: elements in the collection at this level (the number of children in the tree is the size of the set) {processing node; backtracking (path, selection list) / / Recursive backtracking, undo processing result}} "how to find the combination of backtracking algorithm" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Algorithms loops combinations problems violence recursion search content that is articles more knowledge results nodes processing selection good practical successful and then Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Docker Huawei macOS