Skip to content

Spiral Order Matrix II

Problem Description

Given an integer A, generate a square matrix filled with elements from 1 to A2 in spiral order and return the generated square matrix.

Problem Constraints

1 <= A <= 1000
1 <= A <= 1000

Input Format

First and only argument is integer A
First and only argument is integer A

Output Format

 Return a 2-D matrix which consists of the elements added in spiral order.
 Return a 2-D matrix which consists of the elements added in spiral order.

Example Input

Input 1:
1

Input 2:
2

Input 3:
5
Input 1:
1

Input 2:
2

Input 3:
5

Example Output

Output 1:
[ [1] ]

Output 2:
[ [1, 2], 
  [4, 3] ]

Output 3:
[ [1, 2, 3, 4, 5], 
  [16, 17, 18, 19, 6], 
  [15, 24, 25, 20, 7], 
  [14, 23, 22, 21, 8], 
  [13, 12, 11, 10, 9] ]
Output 1:
[ [1] ]

Output 2:
[ [1, 2], 
  [4, 3] ]

Output 3:
[ [1, 2, 3, 4, 5], 
  [16, 17, 18, 19, 6], 
  [15, 24, 25, 20, 7], 
  [14, 23, 22, 21, 8], 
  [13, 12, 11, 10, 9] ]

Example Explanation

Explanation 1:
Only 1 is to be arranged.

Explanation 2:
1 --> 2
      |
      |
4<--- 3
Explanation 3:
Explanation 1:
Only 1 is to be arranged.

Explanation 2:
1 --> 2
      |
      |
4<--- 3
Explanation 3:

Solution

swift
import Foundation

class Solution {
	func generateMatrix(_ A: inout Int) -> [[Int]] {
        var B = [[Int]](repeating:[Int](repeating:0,count:A),count:A)
        var dir = 0 // 0 right 1 down 2 left 3 up
        var x = 0
        var y = -1
        for i in 1...(A * A) {
            if dir == 0 {
               let newY = y + 1
               if newY >= A || B[x][newY] > 0{
                   dir = 1 //down
               } else {
                   B[x][newY] = i
                   y = newY
                   continue
               }
            }
            if dir == 1 {
                 let newX = x + 1
                 if newX >= A || B[newX][y] > 0 {
                     dir = 2
                 } else {
                     B[newX][y] = i
                     x = newX
                     continue
                 }
            }
            if dir == 2 {
                let newY = y - 1
                if newY < 0 || B[x][newY] > 0 {
                    dir = 3
                } else {
                    B[x][newY] = i
                    y = newY
                    continue 
                }
            }
            if dir == 3 {
                 let newX = x - 1
                 if newX >= A || B[newX][y] > 0 {
                     dir = 0
                 } else {
                     B[newX][y] = i
                     x = newX
                     continue
                 }                
            } 
            
            if dir == 0 {
               let newY = y + 1
               if newY >= A || B[x][newY] > 0{
                   break
               } else {
                   B[x][newY] = i
                   y = newY
               }
            }
        }
        
        return B
	}
}
import Foundation

class Solution {
	func generateMatrix(_ A: inout Int) -> [[Int]] {
        var B = [[Int]](repeating:[Int](repeating:0,count:A),count:A)
        var dir = 0 // 0 right 1 down 2 left 3 up
        var x = 0
        var y = -1
        for i in 1...(A * A) {
            if dir == 0 {
               let newY = y + 1
               if newY >= A || B[x][newY] > 0{
                   dir = 1 //down
               } else {
                   B[x][newY] = i
                   y = newY
                   continue
               }
            }
            if dir == 1 {
                 let newX = x + 1
                 if newX >= A || B[newX][y] > 0 {
                     dir = 2
                 } else {
                     B[newX][y] = i
                     x = newX
                     continue
                 }
            }
            if dir == 2 {
                let newY = y - 1
                if newY < 0 || B[x][newY] > 0 {
                    dir = 3
                } else {
                    B[x][newY] = i
                    y = newY
                    continue 
                }
            }
            if dir == 3 {
                 let newX = x - 1
                 if newX >= A || B[newX][y] > 0 {
                     dir = 0
                 } else {
                     B[newX][y] = i
                     x = newX
                     continue
                 }                
            } 
            
            if dir == 0 {
               let newY = y + 1
               if newY >= A || B[x][newY] > 0{
                   break
               } else {
                   B[x][newY] = i
                   y = newY
               }
            }
        }
        
        return B
	}
}

References