给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 1:



输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]

输出:[1,2,3,6,9,8,7,4,5]

示例 2:



输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

输出:[1,2,3,4,8,12,11,10,9,5,6,7]

/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int *spiralOrder(int **matrix, int matrixSize, int *matrixColSize, int *returnSize)
{ int rows = matrixSize, columns = matrixColSize[0];
int total = rows * columns;
int *order = malloc(sizeof(int) * total);
*returnSize = 0;
// 对变量进行抽象,可大大减小指针操作复杂度
int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
while (left <= right && top <= bottom)
{
for (int column = left; column <= right; column++)
{
order[(*returnSize)++] = *(*(matrix + top) + column);
printf("%d\n", *(*(matrix + top) + column));
} for (int row = top + 1; row <= bottom; row++)
{
order[(*returnSize)++] = *(*(matrix + row) + right);
printf("%d\n", *(*(matrix + row) + right));
}
if (left < right && top < bottom)
{
for (int column = right - 1; column > left; column--)
{
order[(*returnSize)++] = *(*(matrix + bottom) + column); } for (int row = bottom; row > top ; row--)
{
order[(*returnSize)++] = *(*(matrix + row) + left); }
}
left++;
right--;
top++;
bottom--;
}
return order;
}

leetcode-螺旋矩阵(指针)的更多相关文章

  1. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. LeetCode:螺旋矩阵||【59】

    LeetCode:螺旋矩阵||[59] 题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ...

  4. LeetCode:螺旋矩阵【54】

    LeetCode:螺旋矩阵[54] 题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], ...

  5. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

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

  6. 【python】Leetcode每日一题-螺旋矩阵2

    [python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...

  7. 【python】Leetcode每日一题-螺旋矩阵

    Leetcode每日一题-螺旋矩阵 [题目描述] 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. 示例1: 输入:matrix = [[1,2,3], ...

  8. LeetCode(59):螺旋矩阵 II

    Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...

  9. LeetCode之螺旋矩阵

    问题 螺旋矩阵 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ ...

  10. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

随机推荐

  1. C# 10 完整特性介绍

    前言 开头防杠:.NET 的基础库.语言.运行时团队从来都是相互独立各自更新的,.NET 6 在基础库.运行时上同样做了非常多的改进,不过本文仅仅介绍语言部分. 距离上次介绍 C# 10 的特性已经有 ...

  2. xubuntu下制作自定义的ISO文件

    by han; 1.打开命令编辑器,建立文件 # sudo su # mkdir -p /opt/custom_conf/user_custom_conf/config 2.将自定义的设置复制保存到/ ...

  3. Android系统编程入门系列之服务Service齐头并进多线程任务

    在上篇文章中初步了解了Android系统的四大组件之一的服务Service,在服务内可以执行无用户交互的耗时操作任务,但是包括之前关于界面系列文章在内,生命周期方法都是在主线程内被系统回调的.如果直接 ...

  4. 使用POI导出Word(含表格)的实现方式及操作Word的工具类

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  5. 批量删除gmail邮件

    以删除tor.com发送的邮件为例说明. 首先点击邮件搜索框右边的三角,在"发件人"下面写上"tor.com": 点"搜索"按钮,看一下范围 ...

  6. 授予mysql的其他用户数据库的使用权限

    场景:不同的开发人员有不同的数据库的权限:也可适用于外包公司不同的开发权限. root用户登录数据库,命令行执行下面语句即可. grant select,delete,update,create,dr ...

  7. 【笔记】scikit-learn中的Scaler(归一化)

    scikit-learn中的数据归一化 在机器学习使用数据归一化的时候有一个重要的注意事项 我们对训练数据进行均值和方差的处理,得到mean_train以及std_train,但是在对测试数据进行归一 ...

  8. spring security 入门级全篇代码

    CustomAccessDecisionManager 类 ---------------------------------------------------------------------- ...

  9. 解决springboot在mac电脑下启动过慢的问题

    自从用了mac以后,springboot启动的时候一直卡在build环节10多秒 但是在linux和Windows环境下,启动只要6秒,后面查看了一下其他 人也遇到这种问题,原来是需要在hosts文件 ...

  10. WPF 显示3D密集场景,堆场管理系统

    又好久好久没写博客了,这次接着上文https://www.cnblogs.com/CSSZBB/p/12785380.html,上文用WPF 的绘图功能,制作了一个伪3D的2.5D控件ThreeDBo ...