作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/spiral-matrix-iii/description/

题目描述

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 walk in a clockwise spiral shape to visit every position in this grid.

Whenever we would 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

题目大意

按照顺时针顺序,把一个矩阵的每个位置遍历一遍。遍历时,圈子越来越大,不能走已经走过了的节点。而且,如果走到了矩阵的外边,那么依然可以走。返回的结果是遍历矩阵节点时的顺序。

解题方法

这个题目和普通的矩阵遍历的区别就是,它可以走到矩阵的外边。但这也降低了难度。

既然如此,那么我们就完全可以直接按照自己的步骤去走就行,当走到矩阵内部的时候,才去添加到结果之中。

所以,使用了一个生成器,每次就生成下一步走到哪了,不考虑外界条件。然后调用这个生成器,判断是否在矩阵之中并且是否所有的位置都已经遍历了。

代码如下:

class Solution(object):
def spiralMatrixIII(self, R, C, r0, c0):
"""
:type R: int
:type C: int
:type r0: int
:type c0: int
:rtype: List[List[int]]
"""
def nxt(r, c):
step = 1
yield (r, c)
while True:
for _ in range(step):
c += 1
yield (r, c)
for _ in range(step):
r += 1
yield (r, c)
step += 1
for _ in range(step):
c -= 1
yield (r, c)
for _ in range(step):
r -= 1
yield (r, c)
step += 1
ans = []
for r, c in nxt(r0, c0):
if 0 <= r < R and 0 <= c < C:
ans.append([r, c])
if len(ans) == R * C:
break
return ans

C++代码如下:

class Solution {
public:
vector<vector<int>> spiralMatrixIII(int R, int C, int r0, int c0) {
vector<vector<int>> res;
int dx = 0, dy = 1;
int step = 1;
int count = 1;
res.push_back(vector<int>{r0, c0});
while (count < R * C) {
dx = 0, dy = 1;
for (int s = 0; s < step; s++) {
r0 += dx;
c0 += dy;
if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) {
res.push_back(vector<int>{r0, c0});
count ++;
if (count >= R * C) return res;
}
}
dx = 1, dy = 0;
for (int s = 0; s < step; s++) {
r0 += dx;
c0 += dy;
if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) {
res.push_back(vector<int>{r0, c0});
count ++;
if (count >= R * C) return res;
}
}
step ++;
dx = 0, dy = -1;
for (int s = 0; s < step; s++) {
r0 += dx;
c0 += dy;
if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) {
res.push_back(vector<int>{r0, c0});
count ++;
if (count >= R * C) return res;
}
}
dx = -1, dy = 0;
for (int s = 0; s < step; s++) {
r0 += dx;
c0 += dy;
if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C) {
res.push_back(vector<int>{r0, c0});
count ++;
if (count >= R * C) return res;
}
}
step ++;
}
return res;
}
};

参考资料:

https://leetcode.com/problems/spiral-matrix-iii/discuss/158995/python-infinite-generator-(yield)

日期

2018 年 9 月 4 日 —— 迎接明媚的阳光!
2018 年 12 月 4 日 —— 周二啦!

【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)的更多相关文章

  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】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

  5. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  6. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  7. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  8. leetcode 889. Spiral Matrix III

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  9. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

随机推荐

  1. nginx——网站显示问题

    一般来说修改3个位置,一个是nginx.h.另一个是ngx_http_header_filter_module.c.还有一个ngx_http_special_response.c. 提示:一般修改都是 ...

  2. mysql—将字符型数字转成数值型数字

    今天写sql语句时,相对字符串类型的数字进行排序,怎么做呢? 需要先转换成数字再进行排序 1.直接用加法 字符串+0 eg: select * from orders order by (mark+0 ...

  3. STM32 BootLoader升级固件

    一.知识点 1.BootLoader就是单片机启动时候运行的一段小程序,这段程序负责单片机固件的更新,也就是单片机选择性的自己给自己下程序.可以更新,也可以不更新,更新的话,BootLoader更新完 ...

  4. tomcat拦截特殊字符报400,如 "|" "{" "}" ","等符号的解决方案

    最近在做一个项目,需要对外暴露两个接口接收别人给的参数,但是有一个问题就是对方的项目是一个老项目,在传参数的时候是将多个字符放在一个参数里面用"|"进行分割,然而他们传参数的时候又 ...

  5. Ubuntu Linux安装QT5之旅

    1. QT 版本选择 如何选择QT版本,参考如下介绍 https://www.cnblogs.com/chinasoft/p/15226293.html 2.  在此以5.15.0解说 下载QT 版本 ...

  6. 23. 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案

    原文:https://blog.csdn.net/u011596455/article/details/60322568 版权声明:本文为博主原创文章,转载请附上博文链接! 在Ubuntu中,有时候运 ...

  7. C++ 之杂记

    今天做了一个题,代码不难,但是编译的时候就恼火,老是报错,也不告诉我错哪了.... 之前的代码是这样的,在main函数中调用这个类的构造函数,就一直报错,但是不知道原因,后来加上了const 就好了. ...

  8. Shell学习(四)——shell中各种括号的作用

    参考博客: [1]shell中各种括号的作用().(()).[].[[]].{} [2]shell中的单层大/中/小括号.双层大中小括号.命令替换等 一.前言 目录 单括号() 双括号(( )) 单中 ...

  9. Output of C++ Program | Set 5

    Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 1 #include<iostream ...

  10. oracle(数据文件)

    --创建数据文件 create tablespace--创建表空间同时创建数据文件 create temporary tablespace --创建临时表空间的同时创建临时数据文件 alter tab ...