给你一个 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. Vulnhub----bulldog靶场笔记

    前提条件 kali和bulldog靶机的的ip地址在同一个网段 本测试环境: kali:192.168.56.102 bulldog:192.168.56.101 主机探测 利用kali的netdis ...

  2. 初识MySQL,关系型数据库&非关系型数据库

    初识MySQL,关系型数据库&非关系型数据库 数据库的分类: 关系型数据库:(SQL) MySQL,Oracle,Sql Server,DB2,SQLlite 通过表和表之间,行和列之间的关系 ...

  3. 跟我一起写 Makefile(十一)

    make 的运行 ------ 一般来说,最简单的就是直接在命令行下输入make命令,make命令会找当前目录的makefile来执行,一切都是自动的.但也有时你也许只想让make重编译某些文件,而不 ...

  4. xml editing in vi

    Auto complete tags xmledit installation: git clone https://github.com/sukima/xmledit.git, then make ...

  5. Linux 内核预备知识:浅析 offsetof 宏以及新手的所思所想

    最近一头扎进了 Linux 内核的学习中,对于我这样一个没什么 C 语言基础的新生代 Java 农民工来说实在太痛苦了.Linux 内核的学习,需要的基础知识太多太多了:C 语言.汇编语言.数据结构与 ...

  6. golang web框架 kratos中的日志框架

    kratos是bilibili开源的一个web框架. 日志用法: logger.go package kratoslog import ( "flag" "github. ...

  7. SpringBoot-2-1-6-集成activiti7-1-0-M4

    pom.xml <dependencyManagement> <dependencies> <dependency> <groupId>org.acti ...

  8. 【springboot】 junit 测试

    参考:https://blog.csdn.net/u012100371/article/details/77206863 @RunWith(SpringJUnit4ClassRunner.class) ...

  9. 菜鸟攻略–C语言多文件编程初探(二):使用 gcc 手动编译多文件 C 程序

    step1:下载安装 Dev-C++ 已经安装了 Dev-C++ 或系统中的可以跳过这步.去官网下载 Dev-C++.我昨天下载,发现有点慢,所以我把安装文件放到百度网盘了,供大家下载,下载链接为:h ...

  10. clojure配置

    1.转换clojure工程为eclipse工程 Install Counterclockwise plugin in Eclipse (from eclipse marketplace). This ...