directory

What’s the difference between a free front end and a salted fish

directory
preface
The story line
The story begins
Ending A
Conclusion B
Conclusion C
Conclusion D
Conclusion E
Take that, Shaolin!

preface

Returns the directory

As an avid gamer of LeetCode every day, I found an event in LeetCode today, so I posted the full walkthrough!

There are 5 good endings in this activity, and the others are Bad ends. Bad ends can only be solved by solving Problem 1024 of LeetCode.

Finally, jsliang puts out a question based on these endings. If you are interested, you can try to challenge it

The story line

Returns the directory

The story begins

Returns the directory

Ending A

Returns the directory

Conclusion B

Returns the directory

Conclusion C

Returns the directory

Conclusion D

Returns the directory

Note: this ending is an endless cycle ~

Conclusion E

Returns the directory

Take that, Shaolin!

Returns the directory

Jsliang gives you a new object:

const obj = {};

obj['A'] = {
  'A': 'Bad End'.'B': {
    'A': {
      'A': 'Bad End'.'B': 'End A'
    },
    'B': 'Bad End',}}; obj['B'] = {
  'A': {
    'A': {
      'A': {
        'A': 'End B'.'B': 'Bad End',},'B': 'Bad End',},'B': 'Bad End',},'B': {
    'A': {
      'A': 'End C'.'B': {
        'A': obj,
        'B': 'Bad End',}},'B': {
      'A': 'End E'.'B': 'Bad End',}}};Copy the code

Please output all successful escape channels (Good End) :

/ * * *@name Get all road maps *@param {object} Obj Roadmap *@return {array} Returns all successful routes, such as [' a-b-a-b ',... * /
const getGoodEnd = (obj) = >{};Copy the code

  • The answer

There are two main points here:

  1. Prevents recursive calls when in a loop at a certain location
  2. Solve the problem by backtracking
/ * * *@name Get all road maps *@param {object} Obj Roadmap *@return {array} Returns all successful routes, such as [' a-b-a-b ',... * /
const getGoodEnd = (obj) = > {

  // 1. Set the hash set
  const set = new Set(a);// 2
  const getType = (param) = > Object.prototype.toString.call(param).slice(8, -1);

  // 3. Set the return result
  const result = [];

  // 4
  const recursion = (obj, path = []) = > {

    // 4.1 Iterate through the current roadmap
    for (let i in obj) {

      // 4.2 If it is an attribute of its own and has not walked this path
      if(obj.hasOwnProperty(i) && ! set.has(obj[i])) {[backtracking] Fill the path first
        path.push(i);

        // 4.4 Determine that the target is an object, then further recurse
        if (getType(obj[i]) === 'Object') {

          // This route has been taken
          set.add(obj[i]);

          // recurse it further
          recursion(obj[i], path);

        } else if (getType(obj[i] === 'String') && obj[i].includes('End ')) {

          // 4.5 If it reaches the End and the string is of the form End X, then it is a good ending
          result.push(path.join(The '-'));
        }

        // 4.6 [backtracking] Push the route you just walked through to facilitate the next passagepath.pop(); }}}; recursion(obj, []);// 5. Return the result
  return result;
};
Copy the code

Do not toss the front end, and what is the difference between salted fish!

Thumbs up/Star if you think the article is good.

If you need to contact jsliang:

  • Github
  • The Denver nuggets

Contact information stored in the Github home page, insist on a LeetCode every day, insist on learning every day, welcome to toss ~



Jsliang document library 由 Liang JunrongusingCreative Commons Attribution – Non-commercial Use – Same way Share 4.0 International LicenseGrant permission.

Based on the
Github.com/LiangJunron…On the creation of works.

Use rights other than those authorized by this License agreement may be obtained from
Creativecommons.org/licenses/by…Obtained.