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;
}
相关推荐
shymoy几秒前
Radix Sorts
数据结构·算法·排序算法
风影小子9 分钟前
注册登录学生管理系统小项目
算法
黑龙江亿林等保11 分钟前
深入探索哈尔滨二级等保下的负载均衡SLB及其核心算法
运维·算法·负载均衡
起名字真南14 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
少年负剑去14 分钟前
第十五届蓝桥杯C/C++B组题解——数字接龙
c语言·c++·蓝桥杯
lucy1530275107914 分钟前
【青牛科技】GC5931:工业风扇驱动芯片的卓越替代者
人工智能·科技·单片机·嵌入式硬件·算法·机器学习
杜杜的man30 分钟前
【go从零单排】迭代器(Iterators)
开发语言·算法·golang
小沈熬夜秃头中୧⍤⃝1 小时前
【贪心算法】No.1---贪心算法(1)
算法·贪心算法
木向1 小时前
leetcode92:反转链表||
数据结构·c++·算法·leetcode·链表
阿阿越1 小时前
算法每日练 -- 双指针篇(持续更新中)
数据结构·c++·算法