Thursday, September 8, 2016

59. Spiral Matrix II


Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
Solution: http://www.programcreek.com/2014/05/leetcode-spiral-matrix-ii-java/
public class Solution {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int top = 0;
        int bottom = n - 1;
        int left = 0;
        int right = n - 1;
        int k = 1;
        
        while (k <= n * n) {
            for (int i = left; i <= right; i++) {
                result[top][i] = k;
                k++;
            }
            
            top++;
            
            for (int i = top; i <= bottom; i++) {
                result[i][right] = k;
                k++;
            }
            
            right--;
            
            for (int i = right; i >= left; i--) {
                result[bottom][i] = k;
                k++;
            }
            
            bottom--;
            
            for (int i = bottom; i >= top; i--) {
                result[i][left] = k;
                k++;
            }
            
            left++;
        }
        
        return result;
    }
}

No comments:

Post a Comment