Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of full permutation in leetcode

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how leetcode solves the problem of full permutation. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Backtracking method (exploration and backtracking method) is a kind of optimal search method, also known as heuristic method, which searches forward according to the optimal conditions in order to achieve the goal. However, when exploring a certain step, it is found that the original choice is not excellent or can not achieve the goal, then go back and choose again. The technique of going back again and again is the retrospective method, and the point of a certain state that meets the backtracking condition is called the "retrospective point".

Full permutation problems, subset problems, combinations and problems are all classical backtracking problems.

Given a sequence with no repeating numbers, return all possible full permutations.

Example:

Input: [1Jing 2Jue 3]

Output:

[

[1,2,3]

[1,3,2]

[2,1,3]

[2,3,1]

[3,1,2]

[3,2,1]

]

Ideas for solving the problem:

1, it can be solved recursively.

2. For a full permutation of length n, for any element, it is spliced with a full permutation of length nmurl

3. Pay attention to the pit of golang slice append

Code:

Func permute (nums [] int) [] [] int {var a [] [] int if len (nums) = = 0 {return a} if len (nums) = = 1 {return append (a lemon nums)} for iRu

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report