首页 > 代码库 > Spiral Matrix II

Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 ton2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

答案

public class Solution {
    public int[][] generateMatrix(int n) {
        if(n<0)
        {
            return null;
        }
        int [][]matrix=new int[n][n];
        int up=0;
        int down=n-1;
        int left=0;
        int right=n-1;
        int row=0;
        int col=0;
        int currentValue=http://www.mamicode.com/1;>

Spiral Matrix II