LeetCode //C - 59. Spiral Matrix II

59. Spiral Matrix II

Given a positive integer n , generate an n x n matrix filled with elements from 1 to n 2 n^2 n2 in spiral order.

Example 1:

Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]

Example 2:

Input: n = 1
Output: [[1]]

Constraints:

  • 1 <= n <= 20

From: LeetCode

Link: 59. Spiral Matrix II


Solution:

Ideas:

1. Initialize Variables:

  • Initialize the 2D array (the matrix) to be returned.
  • Initialize pointers for the number of rows and the size of each row to be returned.
  • Initialize variables for the top, bottom, left, and right boundaries of the current "layer" of the spiral.
  • Initialize a variable val to 1 for the next value to be inserted into the matrix.

2. Loop Until Done:

  • Loop as long as top <= bottom and left <= right.

3. Fill Values:

  • Top Row: Fill the top row from left to right and then increment the top boundary.
  • Right Column: Fill the right column from top to bottom and then decrement the right boundary.
  • Bottom Row: Fill the bottom row from right to left (if top <= bottom) and then decrement the bottom boundary.
  • Left Column: Fill the left column from bottom to top (if left <= right) and then increment the left boundary.

4. Increment Value: After filling each cell, increment the value of val for the next cell.

5. Return the Filled Matrix: Return the 2D array once it's completely filled.

Code:
c 复制代码
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
    // Initialize the output array and sizes
    int** output = (int**)malloc(n * sizeof(int*));
    *returnColumnSizes = (int*)malloc(n * sizeof(int));
    for (int i = 0; i < n; ++i) {
        output[i] = (int*)malloc(n * sizeof(int));
        (*returnColumnSizes)[i] = n;
    }
    *returnSize = n;

    // Initialize variables for boundaries and direction
    int top = 0, bottom = n - 1, left = 0, right = n - 1;
    int val = 1;

    // Loop through the matrix to fill values
    while (top <= bottom && left <= right) {
        // Fill top row
        for (int i = left; i <= right; ++i) {
            output[top][i] = val++;
        }
        ++top;

        // Fill right column
        for (int i = top; i <= bottom; ++i) {
            output[i][right] = val++;
        }
        --right;

        // Fill bottom row
        if (top <= bottom) {
            for (int i = right; i >= left; --i) {
                output[bottom][i] = val++;
            }
            --bottom;
        }

        // Fill left column
        if (left <= right) {
            for (int i = bottom; i >= top; --i) {
                output[i][left] = val++;
            }
            ++left;
        }
    }

    return output;
}
相关推荐
带多刺的玫瑰1 小时前
Leecode刷题C语言之统计不是特殊数字的数字数量
java·c语言·算法
爱敲代码的憨仔1 小时前
《线性代数的本质》
线性代数·算法·决策树
yigan_Eins1 小时前
【数论】莫比乌斯函数及其反演
c++·经验分享·算法
阿史大杯茶1 小时前
AtCoder Beginner Contest 381(ABCDEF 题)视频讲解
数据结构·c++·算法
陌小呆^O^2 小时前
Cmakelist.txt之win-c-udp-server
c语言·开发语言·udp
დ旧言~2 小时前
【高阶数据结构】图论
算法·深度优先·广度优先·宽度优先·推荐算法
时光の尘2 小时前
C语言菜鸟入门·关键字·float以及double的用法
运维·服务器·c语言·开发语言·stm32·单片机·c
张彦峰ZYF2 小时前
投资策略规划最优决策分析
分布式·算法·金融
-一杯为品-2 小时前
【51单片机】程序实验5&6.独立按键-矩阵按键
c语言·笔记·学习·51单片机·硬件工程
The_Ticker2 小时前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程