본문 바로가기

코테

[Programmers] 삼각 달팽이 JS

반응형

방향회전을 n번하고

할때마다 -1씩 개수가 줄어든다

function solution(n) {
    var answer = [];
    let arr = Array.from(new Array(1000), ()=>new Array(1000).fill(0));
    let num = 0;
    let state = 1;
    let x=-1;
    let y=0;
    for(let i=0; i<n; i++){
        if(state===1){
            for(let j=i; j<n; j++){
                arr[++x][y] = ++num;
            }
            state=2;
        }
        else if(state===2){
            for(let j=i; j<n; j++){
                arr[x][++y] = ++num;
            }
            state=3;
        }
        else if(state===3){
            for(let j=i; j<n; j++){
                arr[--x][--y] = ++num;
            }
            state=1;
        }
    }
    for(let i=0; i<n; i++){
        for(let j=0; j<=i ; j++){
            answer.push(arr[i][j]);
        }
    }
    return answer;
}
반응형

'코테' 카테고리의 다른 글

[Programmers] 방문길이 JS  (0) 2022.01.20
[Programmers] 위장 JS  (0) 2022.01.20
[Programmers] 게임 맵 최단거리  (0) 2022.01.19
[Programmers] 가장 큰 수 JS  (0) 2022.01.19
[Programmers] 튜플 JS  (0) 2022.01.19