์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ธํ”„๋Ÿฐ ์„น์…˜8] ๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ (Javascript)

์€์ง„ 2021. 6. 29. 15:18

์ธํ”„๋Ÿฐ

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[์ธํ”„๋Ÿฐ ์„น์…˜8] ๋ถ€๋ถ„์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ (Javascript)
์œ ๋ฃŒ ๊ฐ•์˜์ธ ๊ด€๊ณ„๋กœ ๋ฌธ์ œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.


โœ๏ธIdea Sketch

2021-06-29

1. ์ข…๋ฃŒ์กฐ๊ฑด if (n > 3) return;

2. DFS(n+1); ํ˜ธ์ถœ

3. res.push()๋กœ ๊ตฌํ˜„

4. ์ถœ๋ ฅ์€ ์–ธ์ œ? --> ์ข…๋ฃŒ์กฐ๊ฑด์— ํฌํ•จ

5. n์„ ํฌํ•จํ•˜๊ณ  ๋‚˜์„œ n์„ ํฌํ•จํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์˜ ์ˆ˜


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-29

let res = [];

function DFS(n) {
  if (n > 3) {
    if (res.length > 0) return;
  }
  res.push(n);
  DFS(n + 1);

  res.pop();
  DFS(n + 1);
}

DFS(1);
console.log(res.join(' '))