Simple C# solution


  • 0
    W

    Idea is the same as top voted solutions but condensed

        public int[,] GenerateMatrix(int n) {
            int[,] matrix = new int[n, n];
            int rowStart = 0, rowEnd = n-1, colStart =0, colEnd = n-1;
            int num = 1;
            
            while(rowStart <= rowEnd && colStart <= colEnd)
            {
                for(int i=colStart;i<=colEnd;i++) matrix[rowStart, i] = num++; rowStart++;
                for(int i=rowStart;i<=rowEnd;i++) matrix[i, colEnd] = num++; colEnd--;
                for(int i=colEnd;i>=colStart;i--) matrix[rowEnd, i] = num++; rowEnd--;
                for(int i=rowEnd;i>=rowStart;i--) matrix[i, colStart] = num++; colStart++;
            }
            
            return matrix;
        }
    

Log in to reply
 

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