Day 58 of 100 Days LeetCode Challenge

LeetCode Challenge #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:

spiraln

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

Example 2:

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

 

Constraints:

  • 1 <= n <= 20
Video Solution
Java Solution
				
					class Solution {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int count = 1 ;
        int minRow = 0 ;
        int maxRow = n-1 ;
        int minCol = 0 ;
        int maxCol= n-1 ;
        while(count<=n*n){
            // minRow fixed (minCol --> maxcol );
            for(int c = minCol ; c<=maxCol ; c++ ){
                arr[minRow][c] = count;
                count++;
            }
            // maxCol fixed ( minRow+1 ---> maxRow)
            for(int r=minRow+1 ; r<=maxRow ; r++){
                arr[r][maxCol] = count;
                count++;
            }

            //maxRow fixed (maxcol-1 ---> mincol)
            for(int c = maxCol-1 ; c>=minCol ;c--){
                arr[maxRow][c] = count;
                count++;
            }

            //minCol fixed (maxRow-1 ---> minRow+1 )
            for(int r=maxRow-1 ; r>=minRow+1;r--){
                arr[r][minCol] = count;
                count++;
            }
            minCol++;
            maxCol--;
            minRow++;
            maxRow--;
        }
        return arr ;
    }
}
				
			

Happy Coding with edSlash