My AC simple java solution


  • 0

    the main idea is work from the outermost edges to the middle of the matrix.
    one tricky thing is dealing with the parity of n -- see the comment for details

    public int[][] generateMatrix(int n) {
        int[][] ans = new int[n][n];
        int curr = 1;
        for (int i = 0, j = 0; i<(n+1)/2 ; ++i, ++j){
            for (int k = j; k<n-j; ++k){
                ans[i][k] = curr++;
            }
            if (i>=n/2) break;  // this is needed for odd n's.
            for (int k = i+1; k<n-1-i; ++k){
                ans[k][n-1-j]=curr++;
            }
            for (int k = n-1-j; k>j; --k){
                ans[n-1-i][k] = curr++;
            }
            for (int k = n-1-i; k>i; --k){
                ans[k][j] = curr++;
            }
        }
        
        return ans;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.