leetcode-螺旋矩阵(指针)
给你一个 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-螺旋矩阵(指针)的更多相关文章
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
- LeetCode:螺旋矩阵||【59】
LeetCode:螺旋矩阵||[59] 题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ...
- LeetCode:螺旋矩阵【54】
LeetCode:螺旋矩阵[54] 题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], ...
- [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 ...
- 【python】Leetcode每日一题-螺旋矩阵2
[python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...
- 【python】Leetcode每日一题-螺旋矩阵
Leetcode每日一题-螺旋矩阵 [题目描述] 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. 示例1: 输入:matrix = [[1,2,3], ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- LeetCode之螺旋矩阵
问题 螺旋矩阵 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
随机推荐
- MySQL Replication Thread States
1.主节点线程状态(Replication Master Thread States): Finished reading one binlog; switching to next binlog 线 ...
- 【Lua篇】静态代码扫描分析(三)语法分析
一.语法分析 通过将词法分析获取的Token流按照目标语言的语法进行解析的过程,例如解析函数声明.函数调用.变量声明.各种语句等. 二.Lua语法分析 在写语法分析程序前,先需要了解Lua的语句和语法 ...
- 成为编程大牛很简单,把这些书看个八成就OK
原文链接:http://lucida.me/blog/developer-reading-list/ 本文把程序员所需掌握的关键知识总结为三大类19个关键概念,然后给出了掌握每个关键概念所需的入门书籍 ...
- 【加解密】使用CFSSL生成证书并使用gRPC验证证书
写在前面的话 CFSSL是CloudFlare旗下的PKI/TLS工具.可以用于数字签名,签名验证和TLS证书捆绑的命令行工具和HTTP API服务器. 是使用golang语言开发的证书工具. 官方地 ...
- 嵌入式Linux可用的防火墙——iptables:实现ip白名单、mac地址白名单
iptables是linux系统下的一个功能强大的模块,不仅可以用作防火墙,还可以实现NAT等众多路由功能.iptables的容器有很清晰的层次关系: 1. iptables是表的容器,iptable ...
- 用Vsftpd服务传输文件
文件传输协议 文件传输协议(FTP,File Transfer Protocol),即能够让用户在互联网中上传.下载文件的文件协议,而FTP服务器就是支持FTP传输协议的主机,要想完成文件传输则需要F ...
- Linux 并发服务器编程(多进程)
文章目录 说明 注意事项 server.c client.c 运行截图 说明 在Linux中通过流式套接字编程(TCP),实现一个并发服务器的访问回显,适合刚学完Linux套接字编程的朋友进行巩固训练 ...
- 【C#】GC和析构函数(Finalize 方法)
析构函数: (来自百度百科)析构函数(destructor) 与构造函数相反,当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数.析构函数往往用来做"清理善后&quo ...
- WPF三维立体效果3D
并不是真的3D,类似游戏的2.5D. 先上效果图. 变形一下也可以 起先我是想,把这种绘图啊啥的,都做成控件,给别人直接用就行了.但是做的过程中发现. 要做简单易用的控件,实在是花时间. 而且花的时 ...
- HttpClient4.3教程 第四章 HTTP认证
HttpClient既支持HTTP标准规范定义的认证模式,又支持一些广泛使用的非标准认证模式,比如NTLM和SPNEGO. 4.1.用户凭证 任何用户认证的过程,都需要一系列的凭证来确定用户的身份.最 ...