[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 <= 1001 <= C <= 1000 <= r0 < R0 <= 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 ...
随机推荐
- Rhel6.5 相关操作
Rhel 将光盘挂载动作 操作部分1 挂载光盘 https://jingyan.baidu.com/article/e52e3615a9c19440c60c5121.html ls -l /dev | ...
- 20175236 2018-2019-2 《Java程序设计》第六周学习总结
教材学习内容总结 第七章 try :用于监听.将要被监听的代码(可能抛出异常的代码)放在try语句块之内,当try语句块内发生异常时,异常就被抛出. catch:用于捕获异常.catch用来捕获try ...
- Spring核心模块:IoC容器介绍
1.IoC容器运用的是控制反转模式. 2.IoC容器负责管理对象之间的依赖关系,并完成对象的注入. 3.在IoC设计中,会将依赖关系注入到特定组件中,其中setter注入和构造器注入是主要的注入方式. ...
- c#单例设计模式
class Person { private static Person persons; public static Person Persons { get{ if (persons==null) ...
- 【吴恩达课后编程作业】第二周作业 - Logistic回归-识别猫的图片
1.问题描述 有209张图片作为训练集,50张图片作为测试集,图片中有的是猫的图片,有的不是.每张图片的像素大小为64*64 吴恩达并没有把原始的图片提供给我们 而是把这两个图片集转换成两个.h5文件 ...
- ADT工具使用详解
备注:一下内容为本人手工翻译官方文档注解,如有翻译不到位的地方,欢迎批评指正; ADT(Android开发工具)是Eclipse的插件,它提供了一套与Eclipse IDE集成的工具.它可以让您访问许 ...
- 检测Tensorflow可用设备(比如:显卡)
打开python命令行,输入以下命令: python -c "from tensorflow.python.client import device_lib;device_lib.list_ ...
- C#windows桌面应用小程序制作——大文件数据分段解析存储
现在的任务就是做一个大文件解析的桌面应用小程序,具体需求就是:将一个很大的文件里的数据按一定标志拆分然后分别保存到某个文件夹下面. 解析的文件内容为以下内容: windows 应用小程序界面 具体代码 ...
- websocket client code html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- c语言操作符总结
分类: 算术操作符 移位操作符 位操作符 赋值操作符 单目操作符 关系操作符 逻辑操作符 条件操作符 逗号表达式 其他操作符(下标引用.函数调用和结构成员) 一.算数操作符 1.算术操作符包括:+ ...