๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

์ด์ง„ํŠธ๋ฆฌ ์ˆœํšŒ(DFS : ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm (๊ธฐ์ดˆ๋ฌธ์ œํ’€์ด)

์ด์ง„ํŠธ๋ฆฌ ์ˆœํšŒ(DFS : ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

Daehyunii 2022. 9. 7. 21:45

๋ฌธ์ œ(์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ๊ฐ•์˜, ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ)

์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์ „์œ„์ˆœํšŒ์™€ ํ›„์œ„์ˆœํšŒ๋ฅผ ์—ฐ์Šตํ•ด๋ณด์„ธ์š”.

์ „์œ„์ˆœํšŒ ์ถœ๋ ฅ : 1 2 4 5 3 6 7

์ค‘์œ„์ˆœํšŒ ์ถœ๋ ฅ : 4 2 5 1 6 3 7

ํ›„์œ„์ˆœํšŒ ์ถœ๋ ฅ : 4 5 2 6 7 3 1

 

Tip

 

๋ฌธ์ œํ’€์ด

//๊ฐ•์˜ ๋“ฃ๊ณ  ๋‚ด๊ฐ€ ๋‹ค์‹œ ์ž‘์„ฑํ•œ ๋‹ต
function solution(num){
    let answer = '';
    function DFS(v){
        if(v > 7) return;
        else{
            // answer = answer + String(v) + ' ';
            DFS(v*2);
            answer = answer + String(v) + ' ';
            DFS(v*2+1);
            // answer = answer + String(v) + ' ';
        }
    }
    DFS(num);
    return answer;
}

console.log(solution(1));
console.log(String(1));
console.log(Number('4'));