package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/spiral-matrix-ii/
 */
public class Problem59 {
    public int[][] generateMatrix(int n) {
        return null;
    }
    
    public static void main(String[] args) {
        Problem59 prob = new Problem59();
        for (int[] a : prob.generateMatrix(3)) {
            System.out.println(Arrays.toString(a));
        }
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -151,21 +151,93 @@
     
-return null;%0A
+int%5B%5D%5B%5D matrix = new int%5Bn%5D%5Bn%5D;%0A        if (n == 0) %7B%0A            return matrix;%0A    
     
@@ -246,206 +246,1728 @@
     
-%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem59 prob = new Problem59();%0A        for (int%5B%5D a : prob.generateMatrix(3)) %7B%0A            System.out.println(Arrays.toString(a));%0A        %7D
+Direction%5B%5D directions = new Direction%5B%5D%7B%0A            Direction.LEFT, Direction.DOWN, Direction.RIGHT, Direction.UP%0A        %7D;%0A        int dirIdx = 0;%0A        int row = 0;%0A        int col = 0;%0A        int rowBegin = 0;%0A        int colBegin = 0;%0A        int rowEnd = matrix.length;%0A        int colEnd = matrix%5Brow%5D.length;%0A        int number = 1;%0A        do %7B%0A            if (dirIdx == directions.length) %7B%0A                dirIdx = 0;%0A            %7D%0A            switch (directions%5BdirIdx%5D) %7B%0A            case LEFT:%0A                row = rowBegin;%0A                col = colBegin;%0A                while (col %3C colEnd) %7B%0A                    matrix%5Brow%5D%5Bcol%5D = number++;%0A                    col++;%0A                %7D%0A                rowBegin++;%0A                col--;%0A                break;%0A            case DOWN:%0A                row++;%0A                while (row %3C rowEnd) %7B%0A                    matrix%5Brow%5D%5Bcol%5D = number++;%0A                    row++;%0A                %7D%0A                colEnd--;%0A                row--;%0A                break;%0A            case RIGHT:%0A                col--;%0A                while (col %3E= colBegin) %7B%0A                    matrix%5Brow%5D%5Bcol%5D = number++;%0A                    col--;%0A                %7D%0A                rowEnd--;%0A                col++;%0A                break;%0A            case UP:%0A                row--;%0A                while (row %3E= rowBegin) %7B%0A                    matrix%5Brow%5D%5Bcol%5D = number++;%0A                    row--;%0A                %7D%0A                colBegin++;%0A                break;%0A            %7D%0A            dirIdx++;%0A        %7D while (rowBegin != rowEnd && colBegin != colEnd);%0A        return matrix;%0A    %7D%0A    %0A    private static enum Direction %7B%0A        LEFT, RIGHT, UP, DOWN
 %0A   

Solve problem 59