[Solution] 885. Spiral Matrix Ⅲ
- Difficulty: Medium
Problem
On a 2 dimensional grid with R
rows and C
columns, we start at (r0, c0)
facing east.
Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column.
Now, we talk in a clockwise spiral shape to visit every position in this grid.
Whenever we could move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)
Eventually, we reach all R * C
spaces of the grid.
Return a list of coordinates representing the positions of the grid in the order they were visited.
Example 1:
Input: R = 1, C = 4, r0 = 0, c0 = 0
Output: [[0, 0], [0, 1], [0, 2], [0, 3]]
Example 2:
Input: R = 5, C = 6, r0 = 1, c0 = 4
Output:
[[1, 4], [1, 5],
[2, 5], [2, 4], [2, 3],
[1, 3], [0, 3],
[0, 4], [0, 5],
[3, 5], [3, 4], [3, 3], [3, 2],
[2, 2], [1, 2], [0, 2],
[4, 5], [4, 4], [4, 3], [4, 2], [4, 1],
[3, 1], [2, 1], [1, 1], [0, 1],
[4, 0], [3, 0], [2, 0], [1, 0], [0, 0]]
Note:
1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C
Related Topics
Math
Solution
题意就是给定一个矩阵大小和起始坐标,返回据此条件生成的螺旋方阵的路径,可以走到矩阵之外,但如果坐标落在矩阵之内,则要记录下来。比较容易想到的一个方法是模拟这样的一个走步过程。观察走螺旋的过程,可以发现其在每个方向上走过的距离依次是 [1, 1, 2, 2, 3, 3, ...]
,借助这个规律,就比较好模拟出来了。
public class Solution
{
public int[][] SpiralMatrixIII(int R, int C, int r0, int c0)
{
int[][] ret = new int[R * C][];
int i = 0;
foreach (var coord in NextCoord(r0, c0))
{
if (0 <= coord.Item1 && coord.Item1 < R &&
0 <= coord.Item2 && coord.Item2 < C)
{
ret[i++] = new int[] { coord.Item1, coord.Item2 };
}
if (i == R * C)
break;
}
return ret;
}
private IEnumerable<Tuple<int, int>> NextCoord(int r, int c)
{
int step = 1;
yield return new Tuple<int, int>(r, c);
for( ; ; )
{
for (int i = 0; i < step; i++)
yield return new Tuple<int, int>(r, ++c);
for (int i = 0; i < step; i++)
yield return new Tuple<int, int>(++r, c);
step++;
for (int i = 0; i < step; i++)
yield return new Tuple<int, int>(r, --c);
for (int i = 0; i < step; i++)
yield return new Tuple<int, int>(--r, c);
step++;
}
}
}
- Note:这里使用到了 C♯ 中的
yield
关键字,有关这个的介绍,参见 MSDN 文档。
另外还有一个可行的方法,就是对该矩阵内所有坐标进行排序(首先以其到 (r0, c0)
的距离排序,距离相同的,按照向量 \((r - r_0, c - c_0)^\mathrm{T}\) 与 x 轴正向的夹角大小排序),不过自己似乎写不出这样的代码,留待日后思量。
[Solution] 885. Spiral Matrix Ⅲ的更多相关文章
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- 885. Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
随机推荐
- Office365维护命令
# 会议室日历相关# 日语:予定表# 汉语:日历Set-MailboxFolderPermission -Identity "XXXXX@qq.com:\Calendar" -ac ...
- WEB 性能优化导图
看了一下网上对于web性能优化的一些帖子,不是很直观,花了点时间画了一个思维导图. refers: https://segmentfault.com/a/1190000011936772 https: ...
- HTTP请求的502、504、499错误
1.名词解释 502 Bad Gateway:作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应(伪响应). 504 Gateway Time-out:作为网关或者代理工作的服务 ...
- python_项目_ATM和购物商城的程序
1 需求 模拟实现一个ATM + 购物商城程序 额度15000或自定义 实现购物商城,买东西加入购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 ...
- DP基础练习(4.21)
数塔 Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? ...
- c# 观察者模式 匿名方法与Lambda
//匿名方法 //和委托搭配使用 //方便我们快速对委托进行传参 //不需要我们去定义一个新的函数 //直接用delegate关键字代替方法名,后面跟上参数列表与方法体 //delegate(参数列表 ...
- docker 删除指令
杀死所有正在运行的容器 docker kill $(docker ps -a -q) 删除所有已经停止的容器 docker rm $(docker ps -a -q) 删除所有未打 dangling ...
- Java中的transient关键字
转载于:[lfsf802](http://blog.csdn.net/lfsf802/article/details/43239663) 关键字介绍 一个对象只要实现了Serilizable接口,这个 ...
- 关于RandomAccessFile一个坑!!!!
最近正好遇到了使用RandomAccessFile做断点下载的情况,被一个问题坑了好多次 本来的代码: RandomAccessFile randomAccessFile = new RandomAc ...
- git 每次push和pull都需要提交密码
每次从远程仓库拉代码或者向远程仓库提交代码的时候需要输入密码的解决方法 执行 $ git config --global credential.helper store 结果: 查看C:\用户\用户名 ...