본문 바로가기

Java/Java 알고리즘 LeetCode

[LeetCode- Part. 1] 4. 나선형 매트릭스 생성

반응형

 

https://leetcode.com/problems/spiral-matrix-ii/submissions/

 

Spiral Matrix II - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

참고자료

https://and-some.tistory.com/899

 

[LeetCode- Ch3. 배열] 7. 나선형 매트릭스 #

https://leetcode.com/problems/spiral-matrix/ Spiral Matrix - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 나선형 매트릭스 :m x n

and-some.tistory.com

 

int n이 주어지면 n*n의 나선형 매트릭스를 만든다.


 

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        
        int rowStart=0;
        int rowEnd=n-1;
        
        int colStart=0;
        int colEnd=n-1;
        int tmp=1;
 while(rowStart<=rowEnd && colStart<=colEnd){
     for(int j=colStart; j<=colEnd;j++){
            arr[rowStart][j]=tmp++;
        }
        
        rowStart++;
        for(int i=rowStart;i<=rowEnd;i++){
                arr[i][colEnd]=tmp++;
        }
        colEnd--;
                 if(rowStart<=rowEnd){

        for(int i=colEnd; i>=colStart; i--){
            arr[rowEnd][i]=tmp++;
        }
                 }
        rowEnd--;
                 if(colStart<=colEnd){
        for(int i=rowEnd; i>=rowStart;i--){
            arr[i][colStart]=tmp++;
        }
                 }
        colStart++;
       
 }
        
        return arr;
    }
}

 

 


 

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int rowStart=0;
        int rowEnd=n-1;
        int colStart=0;
        int colEnd=n-1;
        
        int cnt=0;
        while(rowStart<=rowEnd && colStart<=colEnd){
        for(int i=colStart; i<=colEnd;i++){
            arr[rowStart][i]=++cnt;
        }
        rowStart++;
        for(int i=rowStart; i<=rowEnd; i++){
            arr[i][colEnd]=++cnt;
        }
        colEnd--;
        for(int i=colEnd; i>=colStart; i--){
            arr[rowEnd][i]=++cnt;
        }
        rowEnd--;
        for(int i=rowEnd; i>=rowStart; i--){
            arr[i][colStart]=++cnt;
        }
        colStart++;
        }
        
        return arr;
    }
}

 

+) 세련된 풀이

1. 탈출 조건 : while(rowStart<=rowEnd && colStart<=colEnd)

2. for문 조건 :

 //left
for (int i = colEnd; i >= colStart; i --) {
    if (rowStart <= rowEnd)
        matrix[rowEnd][i] = num ++; 
}
rowEnd --;
//up
for (int i = rowEnd; i >= rowStart; i --) {
    if (colStart <= colEnd)
        matrix[i][colStart] = num ++;
}
colStart ++;

 

class Solution {
	public static int[][] generateMatrix(int n) {
        // 1
        int[][] matrix = new int[n][n];
        if (n == 0) return matrix;
        
        int rowStart = 0;
        int rowEnd = n-1;
        int colStart = 0;
        int colEnd = n-1;
        int num = 1; 
        
        //2
        while (rowStart <= rowEnd && colStart <= colEnd) {
        	//right
        	for (int i = colStart; i <= colEnd; i ++) {
                matrix[rowStart][i] = num ++; 
            }
            rowStart ++;
            
            //down
            for (int i = rowStart; i <= rowEnd; i ++) {
                matrix[i][colEnd] = num ++;
            }
            colEnd --;
            
            //left
            for (int i = colEnd; i >= colStart; i --) {
                if (rowStart <= rowEnd)
                    matrix[rowEnd][i] = num ++; 
            }
            rowEnd --;
            //up
            for (int i = rowEnd; i >= rowStart; i --) {
                if (colStart <= colEnd)
                    matrix[i][colStart] = num ++;
            }
            colStart ++;
        }
        
        return matrix;
    }
}
반응형