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 ...
随机推荐
- 2021字节跳动校招秋招算法面试真题解题报告--leetcode19 删除链表的倒数第 n 个结点,内含7种语言答案
2021字节跳动校招秋招算法面试真题解题报告--leetcode19 删除链表的倒数第 n 个结点,内含7种语言答案 1.题目描述 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点. ...
- [SQL]数据更新
插入数据 插入单个元组 一般格式: insert into <表名> [<列名1>, <列明2>, --] -- 指出在表中新插入的值的列, values(< ...
- 遗传算法 TSP(Python代码)
该代码是本人根据B站up主侯昶曦的代码所修改的. 原代码github地址:https://github.com/Houchangxi/heuristic-algorithm/blob/master/T ...
- NOIP 模拟 $18\; \rm 老司机的狂欢$
题解 \(by\;zj\varphi\) 一道很有趣的题,我用的动态开点线段树和倍增 首先对于第一问,不难想到要二分,二分时间,因为时间长一定不会比时间短能跑的人多 那么如何 check,先将所有老司 ...
- MySQL自动填充
一.数据库级别 1.1 表设计 create_time默认值添加CURRENT_TIMESTAMP. update_time默认值添加CURRENT_TIMESTAMP,更新打勾. 1.2 验证是否成 ...
- css内容渐入效果实现
.fade-in-section { opacity: 0; transform: translateY(20vh); visibility: hidden; transition: opacity ...
- mysql 优化面试题
第一方面:30种mysql优化sql语句查询的方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by涉及的列上建立索引. 2.应尽量避免在 where 子句中使用 ...
- wpf Button 动态改变效果
<Button x:Name="LearnMore" Grid.Row="6" HorizontalAlignment="Left&quo ...
- vue项目打包 部署nginx服务器 访问远程接口 本地json 跨域问题
本文建立在你已经在windows7上已经配好了nginx的前提下进行!!! 如果没有请移步至:https://www.cnblogs.com/jack1208-rose0203/p/5739765.h ...
- 多线程同步AutoResetEvent 和ManualResetEvent