엔지니어 게시판
LeetCode 솔루션 분류

59. Spiral Matrix II

컨텐츠 정보

본문

[ Leetcode 시즌 3 ] 2022년도 4월 12일 문제 입니다.


[ Medium ] 59. Spiral Matrix II


Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

 

Example 1:

Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]

Example 2:

Input: n = 1
Output: [[1]]

 

Constraints:

  • 1 <= n <= 20


관련자료

댓글 2

mingki님의 댓글

  • 익명
  • 작성일
C++
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Spiral Matrix II.
Memory Usage: 6.7 MB, less than 18.13% of C++ online submissions for Spiral Matrix II.
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> result(n, vector<int>(n, 0));
        int num = 1;
        
        for (int i = 0; num <= n * n; ++i) {
            for (int j = i; j < n - i; ++j) {
                result[i][j] = num++;
            }
            for (int j = i + 1; j < n - i; ++j) {
                result[j][n - i - 1] = num++;
            }
            for (int j = n - i - 2; j >= 0 + i; --j) {
                result[n - i - 1][j] = num++;
            }
            for (int j = n - i - 2; j >= 0 + i + 1; --j) {
                result[j][0 + i] = num++;
            }
        }
        return result;
    }
};

bobkim님의 댓글

  • 익명
  • 작성일
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Spiral Matrix II.
Memory Usage: 6.4 MB, less than 87.54% of C++ online submissions for Spiral Matrix II.
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> ans(n,vector<int>(n,0));
        
        // 4 steps
        // 1st step : from [0][0] to [0][n-1] - n numbers
        // 2nd step : from [1][n-1] to [n-1][n-1] - n-1 numbers
        // 3rd step : from [n-1][n-2] to [n-1][0] - n-1 numbers
        // 4th step : from [n-2][0] to [1][0] - n-2 numbers
        
        int value = 0;
        int counter=n;
        int step = 0;
        
        while(value<n*n){
            switch(step%4){
                case 0 :
                    for(int i=0;i<counter;i++){
                        value++;
                        ans[step/4][step/4+i]=value;
                    };
                    step++;
                    counter--;
                    break;
                case 1 :
                    for(int i=0;i<counter;i++){
                        value++;
                        ans[step/4+1+i][step/4+counter]=value;
                    };
                    step++;
                    break;
                case 2 :
                    for(int i=0;i<counter;i++){
                        value++;
                        ans[step/4+counter][step/4+counter-1-i]=value;
                    };
                    step++;
                    counter--;
                    break;
                case 3 :
                    for(int i=0;i<counter;i++){
                        value++;
                        ans[step/4+counter-i][step/4]=value;
                    };
                    step++;
                    break;
                default :
                    break;
            };
     
        };
        
        return ans;
    }
};
전체 396 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 242 명
  • 오늘 방문자 2,899 명
  • 어제 방문자 5,663 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,111 명
알림 0