@@ -0,0 +1,1000 @@
+/***%0A * Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.%0A *%0A * For example,%0A * Given n = 3,%0A *%0A * You should return the following matrix:%0A * %5B%0A *  %5B 1, 2, 3 %5D,%0A *  %5B 8, 9, 4 %5D,%0A *  %5B 7, 6, 5 %5D%0A * %5D%0A */%0Apublic class Solution %7B%0A    public int%5B%5D%5B%5D generateMatrix(int n) %7B%0A        int %5B%5D%5B%5D ret = new int %5Bn%5D%5Bn%5D;%0A        if (n == 0) return ret;%0A        int cur = 1, total = n * n;%0A        int x= 0, y = 0; %0A        while (cur %3C= total) %7B%0A            if (n == 1) %7B%0A                ret%5Bx%5D%5By%5D = total;%0A                break;%0A            %7D%0A            for (int i = 0; i %3C n - 1; i++)%0A                ret%5Bx%5D%5By++%5D = cur++;%0A            for (int j = 0; j %3C n - 1; j++)%0A                ret%5Bx++%5D%5By%5D = cur++;%0A            for (int i = 0; i %3C n - 1; i++)%0A                ret%5Bx%5D%5By--%5D = cur++;%0A            for (int j = 0; j %3C n - 1; j++)%0A                ret%5Bx--%5D%5By%5D = cur++;%0A            x += 1;%0A            y += 1;%0A            n -= 2;%0A        %7D%0A        return ret;%0A    %7D%0A%7D%0A

add SpiralMatrixII.java