728x90
SMALL

C#

 

 

using System;

public class Solution {
    public int[] solution(int n) {
        int[] answer = new int[(n*(n+1))/2]; //배열의 총크기
        int[,] temp = new int[n,n];         //2차원 배열

        int x = -1, y = 0;
        int num = 1;

        for (int i = 0; i < n; i++) 
        {
            for (int j = i; j < n; j++) 
            { 
                if (i % 3 == 0) 
                {         //왼쪽대각선으로 진행시
                    x++;
                } 
                else if (i % 3 == 1) 
                {  //오른쪽으로 진행시
                    y++;
                } 
                else if (i % 3 == 2) 
                {  //오른쪽 대각선 진행시
                    x--;
                    y--;
                }
                temp[x,y] = num++;
            }
        }
        
        int k = 0;
        
        for(int i = 0; i < n; i++) 
        {
            for(int j = 0; j < n; j++) 
            {
                if(temp[i,j] == 0) 
                 break;
                answer[k++] = temp[i,j];
            }
        }
        return answer;
    }
}

 

 

 

///////////////////

 

 

using System;

public class Solution
    {
        public int[] solution(int n)
        {
            if (n == 1)
            {
                return new int[] { 1 };
            }
            int[] answer = null;

            int count = n * (n + 1) / 2;
            int[,] arrBoard = new int[n, n];

            int[] xDir = { 1, 0, -1 };
            int[] yDir = { 0, 1, -1 };

            int dir = 0;

            int num = 0;
            int x = 0;
            int y = 0;
            while (num < count)
            {
                num++;
                arrBoard[x, y] = num;

                if (dir == 0 && x + 1 == n)
                {
                    dir += 1;
                }
                if (dir == 1 && y + 1 == n)
                {
                    dir += 1;
                }
                if (arrBoard[x + xDir[dir], y + yDir[dir]] == 0)
                {
                    x = x + xDir[dir];
                    y = y + yDir[dir];
                }
                else
                {
                    dir = (dir + 1) % 3;
                    x = x + xDir[dir];
                    y = y + yDir[dir];
                }
            }

            answer = new int[count];
            int idx = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    answer[idx] = arrBoard[i, j];
                    idx++;
                }
            }

            return answer;
        }
    }

 

 

///////

 

 

자바

 

 

 

class Solution {
    public int[] solution(int n) {
        int[][] li = new int[n][n];
        int x=0,y=0;
        int step = n;
        int value=1;
        while(step >0){
            /*아래로 이동 */
            for(int i=step;i>0;i--){
                li[x++][y] = value++;
            }
            x--; // 행 복귀 
            y++; // 로테이션
            step--; // 스텝 업데이트

            /* 오른쪽 이동 */
            for(int i=step;i>0;i--){
                li[x][y++] = value++;
            }
            y--; // 열 복귀
            x--; // 로테이션
            y--; // 로테이션
            step--;

            /* 북서쪽 이동 */
            for(int i=step;i>0;i--){
                li[x--][y--] = value++;
            }
            x++; // 행 복귀
            y++; // 열 복귀
            x++; // 로테이션
            step--;
        }

        int[] answer = new int[n*(n+1)/2];
        int size=0;
        
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(li[i][j]==0) break;
                answer[size++] = li[i][j];
            }
        }
        return answer;
    }
}

 

 

 

class Solution {
    public int[] solution(int n) {
        int[] answer = new int[(n*(n+1))/2];
        int[][] matrix = new int[n][n];

        int x = -1, y = 0;
        int num = 1;

        for (int i = 0; i < n; ++i) {
            for (int j = i; j < n; ++j) {
                if (i % 3 == 0) {
                    ++x;
                } else if (i % 3 == 1) {
                    ++y;
                } else if (i % 3 == 2) {
                    --x;
                    --y;
                }
                matrix[x][y] = num++;
            }
        }

        int k = 0;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                if(matrix[i][j] == 0) break;
                answer[k++] = matrix[i][j];
            }
        }

        return answer;
    }
}
728x90

설정

트랙백

댓글