• 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. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 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 Ⅲ的更多相关文章

  1. [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 ...

  2. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  3. 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 ...

  4. 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  8. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. 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 ...

随机推荐

  1. mybatis的逆向工程和中文注解

    由于MyBatis Generator自带了生成注释的功能,但是,是英文的而且生成的根本无法理解,所以可以通过,修改他的源码来实现生成中文的注释,具体方式有以下几种: 1) 自定义CommentGen ...

  2. ubuntu16.04 HyperLedger Fabric 1.2.0 开发环境搭建

    安装准备 1. 安装git.cRUL.gcc/g++和make $ sudo apt-get update $ sudo apt-get install build-essential git cur ...

  3. excle 填充单元格内容到相同长度

    =CONCATENATE(REPT("0",4-LEN(H1)),H1)     

  4. 20175236 2018-2019-2 《Java程序设计》第七周学习总结

    教材学习内容总结 String类 String类在java.lang包中的类被默认引入,因此程序可以直接使用String类 Java把String类定义为final类,因此用户不能扩展String类, ...

  5. vs2017使用问题

    最近安装了新版本的Visual studio  2017,但是在使用的过程中遇到了这样一个问题.刚启动电脑后,打开vs2017是可以打开的,但是当关掉之后再打开就打不开了,但是任务管理器看可以看到有一 ...

  6. List<Map<String, Object>>集合中获取某个key并转换为List<Integer>集合

    package com.codyy.sso.controller.yuanqu; import java.util.ArrayList; import java.util.HashMap; impor ...

  7. 2018/12/19 20:55:58 螺纹钢豆粕PTA

    螺纹钢M5中枢上升到M30级别,感觉向上的可能高..可是没有好的开仓位,那就不用硬要开仓,耐心等待自己熟悉的信号: PTA M5中枢扩展为M30中枢,目前M30向下一笔没结束: 豆粕等待当前日线下跌结 ...

  8. Java——@SupressWarnings

    J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. 背景:J2SE 5.0 为 Java 语言增加了几 ...

  9. 使用JavaMail创建邮件和发送邮件

    参考https://www.cnblogs.com/xdp-gacl/p/4216311.html,写的真好,知识在于分享,备份留着看 一.RFC882文档简单说明 RFC882文档规定了如何编写一封 ...

  10. 利用JAVA API函数实现数据的压缩与解压缩

      综述 许多信息资料都或多或少的包含一些多余的数据.通常会导致在客户端与服务器之间,应用程序与计算机之间极大的数据传输量.最常见的解决数据存储和信息传送的方法是安装额外的存储设备和扩展现有的通讯能力 ...