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;
}
相关推荐
Pniubi5 分钟前
力扣3无重复字符的最长字串(滑动窗口)
算法·leetcode·职场和发展
On The Road :15 分钟前
深入理解 JVM 垃圾回收:GC 是什么、如何判定垃圾、四大回收算法全解析
jvm·算法
智者知已应修善业1 小时前
【51单片机实现从下到上,从上到下,从中间到两边,两边到中间】2024-10-23
c语言·经验分享·笔记·单片机·嵌入式硬件·51单片机
微尘hjx1 小时前
【Deep-OC-SORT 03】多轨迹算法Deep-OC-SORT-卡尔曼滤波预测过程
算法·yolo·目标跟踪·ultralytics·车跟踪·轨迹算法·deep-oc-sort
老师我太想进步了20263 小时前
【数据结构-3】算法美学之降复杂度
数据结构·算法
机器人零零壹8 小时前
技术白皮书:iRobotCAM如何解决机器人激光切割应用中的核心难点,从CAD离线编程到双目视觉定位与轨迹自适应提升复杂空间曲线切割的精度、效率与柔性化水平
人工智能·算法
布莱克60510 小时前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
ZhengEnCi11 小时前
大模型的分类到底怎么分:从基础模型到推理模型、从通用到专用的完整图谱
算法
取名真是12 小时前
Python 算法知识点从基础到进阶
开发语言·python·算法
罗西的思考12 小时前
【轻量化复刻大模型 / KIMI K3】KDA 模块交互式演示
人工智能·算法·机器学习